
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.local_env &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.local_env</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.local_env</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module provides classes to perform analyses of</span>
<span class="sd">the local environments (e.g., finding near neighbors)</span>
<span class="sd">of single sites in molecules and structures.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">json</span>

<span class="kn">import</span> <span class="nn">ruamel.yaml</span> <span class="k">as</span> <span class="nn">yaml</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">deepcopy</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="nb">pow</span><span class="p">,</span> <span class="n">pi</span><span class="p">,</span> <span class="n">asin</span><span class="p">,</span> <span class="n">sqrt</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">sin</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">acos</span><span class="p">,</span> <span class="n">fabs</span><span class="p">,</span> <span class="n">atan2</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span><span class="p">,</span> <span class="n">defaultdict</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">lru_cache</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">bisect</span> <span class="kn">import</span> <span class="n">bisect_left</span>

<span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="kn">import</span> <span class="n">Voronoi</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">deprecated</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">requires</span>
<span class="kn">from</span> <span class="nn">monty.serialization</span> <span class="kn">import</span> <span class="n">loadfn</span>

<span class="kn">from</span> <span class="nn">pymatgen</span> <span class="kn">import</span> <span class="n">Element</span><span class="p">,</span> <span class="n">Structure</span><span class="p">,</span> <span class="n">IStructure</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.bond_valence</span> <span class="kn">import</span> <span class="n">BV_PARAMS</span><span class="p">,</span> <span class="n">BVAnalyzer</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.structure</span> <span class="kn">import</span> <span class="n">PeriodicNeighbor</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.molecule_structure_comparator</span> <span class="kn">import</span> <span class="n">CovalentRadius</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.sites</span> <span class="kn">import</span> <span class="n">PeriodicSite</span><span class="p">,</span> <span class="n">Site</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">openbabel</span> <span class="kn">import</span> <span class="n">openbabel</span> <span class="k">as</span> <span class="n">ob</span>
<span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
    <span class="n">ob</span> <span class="o">=</span> <span class="kc">None</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong, Geoffroy Hautier, Sai Jayaraman,&quot;</span> <span class="o">+</span> \
             <span class="s2">&quot; Nils E. R. Zimmermann, Bharat Medasani, Evan Spotte-Smith&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2011, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Nils E. R. Zimmermann&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;nils.e.r.zimmermann@gmail.com&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;August 17, 2017&quot;</span>

<span class="n">_directory</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>

<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_directory</span><span class="p">,</span> <span class="s1">&#39;op_params.yaml&#39;</span><span class="p">),</span> <span class="s2">&quot;rt&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">default_op_params</span> <span class="o">=</span> <span class="n">yaml</span><span class="o">.</span><span class="n">safe_load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_directory</span><span class="p">,</span> <span class="s1">&#39;cn_opt_params.yaml&#39;</span><span class="p">),</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">cn_opt_params</span> <span class="o">=</span> <span class="n">yaml</span><span class="o">.</span><span class="n">safe_load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_directory</span><span class="p">,</span> <span class="s1">&#39;ionic_radii.json&#39;</span><span class="p">),</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
    <span class="n">_ion_radii</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>


<div class="viewcode-block" id="ValenceIonicRadiusEvaluator"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.ValenceIonicRadiusEvaluator">[docs]</a><span class="k">class</span> <span class="nc">ValenceIonicRadiusEvaluator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Computes site valences and ionic radii for a structure using bond valence</span>
<span class="sd">    analyzer</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen.core.structure.Structure</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_valences</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_valences</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_ionic_radii</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_ionic_radii</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">radii</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of ionic radii of elements in the order of sites.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">el</span> <span class="o">=</span> <span class="p">[</span><span class="n">site</span><span class="o">.</span><span class="n">species_string</span> <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">sites</span><span class="p">]</span>
        <span class="n">radii_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">el</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ionic_radii</span><span class="p">))</span>
        <span class="c1"># print radii_dict</span>
        <span class="k">return</span> <span class="n">radii_dict</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">valences</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of oxidation states of elements in the order of sites.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">el</span> <span class="o">=</span> <span class="p">[</span><span class="n">site</span><span class="o">.</span><span class="n">species_string</span> <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">sites</span><span class="p">]</span>
        <span class="n">valence_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">el</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_valences</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">valence_dict</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structure</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns oxidation state decorated structure.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_get_ionic_radii</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes ionic radii of elements for all sites in the structure.</span>
<span class="sd">        If valence is zero, atomic radius is used.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">radii</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">vnn</span> <span class="o">=</span> <span class="n">VoronoiNN</span><span class="p">()</span>

        <span class="k">def</span> <span class="nf">nearest_key</span><span class="p">(</span><span class="n">sorted_vals</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
            <span class="n">i</span> <span class="o">=</span> <span class="n">bisect_left</span><span class="p">(</span><span class="n">sorted_vals</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">sorted_vals</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">sorted_vals</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">sorted_vals</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">before</span> <span class="o">=</span> <span class="n">sorted_vals</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
            <span class="n">after</span> <span class="o">=</span> <span class="n">sorted_vals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">after</span> <span class="o">-</span> <span class="n">key</span> <span class="o">&lt;</span> <span class="n">key</span> <span class="o">-</span> <span class="n">before</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">after</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">before</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">sites</span><span class="p">)):</span>
            <span class="n">site</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">sites</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">,</span> <span class="n">Element</span><span class="p">):</span>
                <span class="n">radius</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">atomic_radius</span>
                <span class="c1"># Handle elements with no atomic_radius</span>
                <span class="c1"># by using calculated values instead.</span>
                <span class="k">if</span> <span class="n">radius</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">radius</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">atomic_radius_calculated</span>
                <span class="k">if</span> <span class="n">radius</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;cannot assign radius to element </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">))</span>
                <span class="n">radii</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">radius</span><span class="p">)</span>
                <span class="k">continue</span>

            <span class="n">el</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">symbol</span>
            <span class="n">oxi_state</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">oxi_state</span><span class="p">))</span>
            <span class="n">coord_no</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">vnn</span><span class="o">.</span><span class="n">get_cn</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="p">,</span> <span class="n">i</span><span class="p">)))</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">tab_oxi_states</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>
                <span class="n">oxi_state</span> <span class="o">=</span> <span class="n">nearest_key</span><span class="p">(</span><span class="n">tab_oxi_states</span><span class="p">,</span> <span class="n">oxi_state</span><span class="p">)</span>
                <span class="n">radius</span> <span class="o">=</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">oxi_state</span><span class="p">)][</span><span class="nb">str</span><span class="p">(</span><span class="n">coord_no</span><span class="p">)]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">vnn</span><span class="o">.</span><span class="n">get_cn</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="o">-</span> <span class="n">coord_no</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">new_coord_no</span> <span class="o">=</span> <span class="n">coord_no</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">new_coord_no</span> <span class="o">=</span> <span class="n">coord_no</span> <span class="o">-</span> <span class="mi">1</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">radius</span> <span class="o">=</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">oxi_state</span><span class="p">)][</span><span class="nb">str</span><span class="p">(</span><span class="n">new_coord_no</span><span class="p">)]</span>
                    <span class="n">coord_no</span> <span class="o">=</span> <span class="n">new_coord_no</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                    <span class="n">tab_coords</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span>
                        <span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">oxi_state</span><span class="p">)]</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>
                    <span class="n">new_coord_no</span> <span class="o">=</span> <span class="n">nearest_key</span><span class="p">(</span><span class="n">tab_coords</span><span class="p">,</span> <span class="n">coord_no</span><span class="p">)</span>
                    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">tab_coords</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">val</span> <span class="o">&gt;</span> <span class="n">coord_no</span><span class="p">:</span>
                            <span class="k">break</span>
                        <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">tab_coords</span><span class="p">):</span>
                        <span class="n">key</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">tab_coords</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                        <span class="n">radius</span> <span class="o">=</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">oxi_state</span><span class="p">)][</span><span class="n">key</span><span class="p">]</span>
                    <span class="k">elif</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">key</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">tab_coords</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                        <span class="n">radius</span> <span class="o">=</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">oxi_state</span><span class="p">)][</span><span class="n">key</span><span class="p">]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">key</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">tab_coords</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>
                        <span class="n">radius1</span> <span class="o">=</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">oxi_state</span><span class="p">)][</span><span class="n">key</span><span class="p">]</span>
                        <span class="n">key</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">tab_coords</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                        <span class="n">radius2</span> <span class="o">=</span> <span class="n">_ion_radii</span><span class="p">[</span><span class="n">el</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">oxi_state</span><span class="p">)][</span><span class="n">key</span><span class="p">]</span>
                        <span class="n">radius</span> <span class="o">=</span> <span class="p">(</span><span class="n">radius1</span> <span class="o">+</span> <span class="n">radius2</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>

            <span class="c1"># implement complex checks later</span>
            <span class="n">radii</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">radius</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">radii</span>

    <span class="k">def</span> <span class="nf">_get_valences</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes ionic valences of elements for all sites in the structure.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">bv</span> <span class="o">=</span> <span class="n">BVAnalyzer</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span> <span class="o">=</span> <span class="n">bv</span><span class="o">.</span><span class="n">get_oxi_state_decorated_structure</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="p">)</span>
            <span class="n">valences</span> <span class="o">=</span> <span class="n">bv</span><span class="o">.</span><span class="n">get_valences</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">bv</span> <span class="o">=</span> <span class="n">BVAnalyzer</span><span class="p">(</span><span class="n">symm_tol</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span> <span class="o">=</span> <span class="n">bv</span><span class="o">.</span><span class="n">get_oxi_state_decorated_structure</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="p">)</span>
                <span class="n">valences</span> <span class="o">=</span> <span class="n">bv</span><span class="o">.</span><span class="n">get_valences</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="n">valences</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">sites</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">common_oxidation_states</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">valences</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">common_oxidation_states</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                    <span class="c1"># Handle noble gas species</span>
                    <span class="c1"># which have no entries in common_oxidation_states.</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">valences</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">sum</span><span class="p">(</span><span class="n">valences</span><span class="p">):</span>
                    <span class="n">valences</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">num_sites</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_structure</span><span class="o">.</span><span class="n">add_oxidation_state_by_site</span><span class="p">(</span><span class="n">valences</span><span class="p">)</span>
                <span class="c1"># raise</span>

        <span class="c1"># el = [site.specie.symbol for site in self._structure.sites]</span>
        <span class="c1"># el = [site.species_string for site in self._structure.sites]</span>
        <span class="c1"># el = [site.specie for site in self._structure.sites]</span>
        <span class="c1"># valence_dict = dict(zip(el, valences))</span>
        <span class="c1"># print valence_dict</span>
        <span class="k">return</span> <span class="n">valences</span></div>


<div class="viewcode-block" id="NearNeighbors"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors">[docs]</a><span class="k">class</span> <span class="nc">NearNeighbors</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class to determine near neighbors that typically include nearest</span>
<span class="sd">    neighbors and others that are within some tolerable distance.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">other</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="vm">__dict__</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;structures_allowed&quot;</span>
                                  <span class="s2">&quot; is not defined!&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;molecules_allowed&quot;</span>
                                  <span class="s2">&quot; is not defined!&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;extend_structures_molecule&quot;</span>
                                  <span class="s2">&quot; is not defined!&quot;</span><span class="p">)</span>

<div class="viewcode-block" id="NearNeighbors.get_cn"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_cn">[docs]</a>    <span class="k">def</span> <span class="nf">get_cn</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">use_weights</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get coordination number, CN, of site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine CN.</span>
<span class="sd">            use_weights (boolean): flag indicating whether (True)</span>
<span class="sd">                to use weights for computing the coordination number</span>
<span class="sd">                or not (False, default: each coordinated site has equal</span>
<span class="sd">                weight).</span>
<span class="sd">        Returns:</span>
<span class="sd">            cn (integer or float): coordination number.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">e</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">siw</span><span class="p">])</span> <span class="k">if</span> <span class="n">use_weights</span> <span class="k">else</span> <span class="nb">len</span><span class="p">(</span><span class="n">siw</span><span class="p">)</span></div>

<div class="viewcode-block" id="NearNeighbors.get_cn_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_cn_dict">[docs]</a>    <span class="k">def</span> <span class="nf">get_cn_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">use_weights</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get coordination number, CN, of each element bonded to site with index n in structure</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure</span>
<span class="sd">            n (integer): index of site for which to determine CN.</span>
<span class="sd">            use_weights (boolean): flag indicating whether (True)</span>
<span class="sd">                to use weights for computing the coordination number</span>
<span class="sd">                or not (False, default: each coordinated site has equal</span>
<span class="sd">                weight).</span>

<span class="sd">        Returns:</span>
<span class="sd">            cn (dict): dictionary of CN of each element bonded to site</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>

        <span class="n">cn_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">siw</span><span class="p">:</span>
            <span class="n">site_element</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">species_string</span>
            <span class="k">if</span> <span class="n">site_element</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">cn_dict</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">use_weights</span><span class="p">:</span>
                    <span class="n">cn_dict</span><span class="p">[</span><span class="n">site_element</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">cn_dict</span><span class="p">[</span><span class="n">site_element</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">use_weights</span><span class="p">:</span>
                    <span class="n">cn_dict</span><span class="p">[</span><span class="n">site_element</span><span class="p">]</span> <span class="o">+=</span> <span class="n">i</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">cn_dict</span><span class="p">[</span><span class="n">site_element</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">cn_dict</span></div>

<div class="viewcode-block" id="NearNeighbors.get_nn"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_nn">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get near neighbors of site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site in structure for which to determine</span>
<span class="sd">                    neighbors.</span>
<span class="sd">        Returns:</span>
<span class="sd">            sites (list of Site objects): near neighbors.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">[</span><span class="n">e</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)]</span></div>

<div class="viewcode-block" id="NearNeighbors.get_weights_of_nn_sites"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_weights_of_nn_sites">[docs]</a>    <span class="k">def</span> <span class="nf">get_weights_of_nn_sites</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get weight associated with each near neighbor of site with</span>
<span class="sd">        index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine the weights.</span>
<span class="sd">        Returns:</span>
<span class="sd">            weights (list of floats): near-neighbor weights.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">[</span><span class="n">e</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)]</span></div>

<div class="viewcode-block" id="NearNeighbors.get_nn_images"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_nn_images">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_images</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get image location of all near neighbors of site with index n in</span>
<span class="sd">        structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine the image</span>
<span class="sd">                location of near neighbors.</span>
<span class="sd">        Returns:</span>
<span class="sd">            images (list of 3D integer array): image locations of</span>
<span class="sd">                near neighbors.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">[</span><span class="n">e</span><span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)]</span></div>

<div class="viewcode-block" id="NearNeighbors.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                information.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of dicts): each dictionary provides information</span>
<span class="sd">                about a single near neighbor, where key &#39;site&#39; gives</span>
<span class="sd">                access to the corresponding Site object, &#39;image&#39; gives</span>
<span class="sd">                the image location, and &#39;weight&#39; provides the weight</span>
<span class="sd">                that a given near-neighbor site contributes</span>
<span class="sd">                to the coordination number (1 or smaller), &#39;site_index&#39;</span>
<span class="sd">                gives index of the corresponding site in</span>
<span class="sd">                the original structure.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;get_nn_info(structure, n)&quot;</span>
                                  <span class="s2">&quot; is not defined!&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="NearNeighbors.get_all_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_all_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_all_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get a listing of all neighbors for all sites in a structure</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure</span>
<span class="sd">        Return:</span>
<span class="sd">            List of NN site information for each site in the structure. Each</span>
<span class="sd">                entry has the same format as `get_nn_info`</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">))]</span></div>

<div class="viewcode-block" id="NearNeighbors.get_nn_shell_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_nn_shell_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_shell_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">shell</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get a certain nearest neighbor shell for a certain site.</span>

<span class="sd">        Determines all non-backtracking paths through the neighbor network</span>
<span class="sd">        computed by `get_nn_info`. The weight is determined by multiplying</span>
<span class="sd">        the weight of the neighbor at each hop through the network. For</span>
<span class="sd">        example, a 2nd-nearest-neighbor that has a weight of 1 from its</span>
<span class="sd">        1st-nearest-neighbor and weight 0.5 from the original site will</span>
<span class="sd">        be assigned a weight of 0.5.</span>

<span class="sd">        As this calculation may involve computing the nearest neighbors of</span>
<span class="sd">        atoms multiple times, the calculation starts by computing all of the</span>
<span class="sd">        neighbor info and then calling `_get_nn_shell_info`. If you are likely</span>
<span class="sd">        to call this method for more than one site, consider calling `get_all_nn`</span>
<span class="sd">        first and then calling this protected method yourself.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure</span>
<span class="sd">            site_idx (int): index of site for which to determine neighbor</span>
<span class="sd">                information.</span>
<span class="sd">            shell (int): Which neighbor shell to retrieve (1 == 1st NN shell)</span>
<span class="sd">        Returns:</span>
<span class="sd">            list of dictionaries. Each entry in the list is information about</span>
<span class="sd">                a certain neighbor in the structure, in the same format as</span>
<span class="sd">                `get_nn_info`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">all_nn_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="n">sites</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_nn_shell_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">all_nn_info</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">shell</span><span class="p">)</span>

        <span class="c1"># Update the site positions</span>
        <span class="c1">#   Did not do this during NN options because that can be slower</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">info</span> <span class="ow">in</span> <span class="n">sites</span><span class="p">:</span>
            <span class="n">orig_site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">info</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">]]</span>
            <span class="n">info</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">PeriodicSite</span><span class="p">(</span><span class="n">orig_site</span><span class="o">.</span><span class="n">species</span><span class="p">,</span>
                                        <span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">orig_site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">,</span>
                                               <span class="n">info</span><span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">]),</span>
                                        <span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="p">,</span>
                                        <span class="n">properties</span><span class="o">=</span><span class="n">orig_site</span><span class="o">.</span><span class="n">properties</span><span class="p">)</span>
            <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span></div>

    <span class="k">def</span> <span class="nf">_get_nn_shell_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">all_nn_info</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">shell</span><span class="p">,</span>
                           <span class="n">_previous_steps</span><span class="o">=</span><span class="nb">frozenset</span><span class="p">(),</span> <span class="n">_cur_image</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)):</span>
        <span class="sd">&quot;&quot;&quot;Private method for computing the neighbor shell information</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure) - Structure being assessed</span>
<span class="sd">            all_nn_info ([[dict]]) - Results from `get_all_nn_info`</span>
<span class="sd">            site_idx (int) - index of site for which to determine neighbor</span>
<span class="sd">                information.</span>
<span class="sd">            shell (int) - Which neighbor shell to retrieve (1 == 1st NN shell)</span>
<span class="sd">            _previous_step ({(site_idx, image}) - Internal use only: Set of</span>
<span class="sd">                sites that have already been traversed.</span>
<span class="sd">            _cur_image (tuple) - Internal use only Image coordinates of current atom</span>
<span class="sd">        Returns:</span>
<span class="sd">            list of dictionaries. Each entry in the list is information about</span>
<span class="sd">                a certain neighbor in the structure, in the same format as</span>
<span class="sd">                `get_nn_info`. Does not update the site positions</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">shell</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Shell must be positive&#39;</span><span class="p">)</span>

        <span class="c1"># Append this site to the list of previously-visited sites</span>
        <span class="n">_previous_steps</span> <span class="o">=</span> <span class="n">_previous_steps</span><span class="o">.</span><span class="n">union</span><span class="p">({(</span><span class="n">site_idx</span><span class="p">,</span> <span class="n">_cur_image</span><span class="p">)})</span>

        <span class="c1"># Get all the neighbors of this site</span>
        <span class="n">possible_steps</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">all_nn_info</span><span class="p">[</span><span class="n">site_idx</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">step</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">possible_steps</span><span class="p">):</span>
            <span class="c1"># Update the image information</span>
            <span class="c1">#  Note: We do not update the site position yet, as making a</span>
            <span class="c1">#    PeriodicSite for each intermediate step is too costly</span>
            <span class="n">step</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
            <span class="n">step</span><span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">step</span><span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">],</span> <span class="n">_cur_image</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
            <span class="n">possible_steps</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">step</span>

        <span class="c1"># Get only the non-backtracking steps</span>
        <span class="n">allowed_steps</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">possible_steps</span> <span class="k">if</span>
                         <span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">],</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">])</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">_previous_steps</span><span class="p">]</span>

        <span class="c1"># If we are the last step (i.e., shell == 1), done!</span>
        <span class="k">if</span> <span class="n">shell</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># No further work needed, just package these results</span>
            <span class="k">return</span> <span class="n">allowed_steps</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># If not, Get the N-1 NNs of these allowed steps</span>
            <span class="n">terminal_neighbors</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_nn_shell_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                          <span class="n">all_nn_info</span><span class="p">,</span>
                                                          <span class="n">x</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">],</span>
                                                          <span class="n">shell</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span>
                                                          <span class="n">_previous_steps</span><span class="p">,</span>
                                                          <span class="n">x</span><span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">])</span>
                                  <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">allowed_steps</span><span class="p">]</span>

            <span class="c1"># Each allowed step results in many terminal neighbors</span>
            <span class="c1">#  And, different first steps might results in the same neighbor</span>
            <span class="c1">#  Now, we condense those neighbors into a single entry per neighbor</span>
            <span class="n">all_sites</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">first_site</span><span class="p">,</span> <span class="n">term_sites</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">allowed_steps</span><span class="p">,</span>
                                              <span class="n">terminal_neighbors</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">term_site</span> <span class="ow">in</span> <span class="n">term_sites</span><span class="p">:</span>
                    <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">term_site</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">],</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">term_site</span><span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">]))</span>

                    <span class="c1"># The weight for this site is equal to the weight of the</span>
                    <span class="c1">#  first step multiplied by the weight of the terminal neighbor</span>
                    <span class="n">term_site</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span> <span class="o">*=</span> <span class="n">first_site</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span>

                    <span class="c1"># Check if this site is already known</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="n">all_sites</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="c1"># If so, add to its weight</span>
                        <span class="n">value</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">term_site</span><span class="p">[</span><span class="s1">&#39;weight&#39;</span><span class="p">]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="c1"># If not, prepare to add it</span>
                        <span class="n">value</span> <span class="o">=</span> <span class="n">term_site</span>
                    <span class="n">all_sites</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">all_sites</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">site</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Private convenience method for get_nn_info,</span>
<span class="sd">        gives lattice image from provided PeriodicSite and Structure.</span>

<span class="sd">        Image is defined as displacement from original site in structure to a given site.</span>
<span class="sd">        i.e. if structure has a site at (-0.1, 1.0, 0.3), then (0.9, 0, 2.3) -&gt; jimage = (1, -1, 2).</span>
<span class="sd">        Note that this method takes O(number of sites) due to searching an original site.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: Structure Object</span>
<span class="sd">            site: PeriodicSite Object</span>

<span class="sd">        Returns:</span>
<span class="sd">            image: ((int)*3) Lattice image</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">original_site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span>
            <span class="n">NearNeighbors</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">site</span><span class="p">)]</span>
        <span class="n">image</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span>
            <span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">,</span> <span class="n">original_site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">))</span>
        <span class="n">image</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">image</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">image</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">site</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Private convenience method for get_nn_info,</span>
<span class="sd">        gives original site index from ProvidedPeriodicSite.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">site</span><span class="o">.</span><span class="n">is_periodic_image</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">i</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;Site not found!&#39;</span><span class="p">)</span>

<div class="viewcode-block" id="NearNeighbors.get_bonded_structure"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_bonded_structure">[docs]</a>    <span class="k">def</span> <span class="nf">get_bonded_structure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">decorate</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Obtain a StructureGraph object using this NearNeighbor</span>
<span class="sd">        class. Requires the optional dependency networkx</span>
<span class="sd">        (pip install networkx).</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: Structure object.</span>
<span class="sd">            decorate (bool): whether to annotate site properties</span>
<span class="sd">            with order parameters using neighbors determined by</span>
<span class="sd">            this NearNeighbor class</span>
<span class="sd">            weights (bool): whether to include edge weights from</span>
<span class="sd">            NearNeighbor class in StructureGraph</span>

<span class="sd">        Returns: a pymatgen.analysis.graphs.StructureGraph object</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># requires optional dependency which is why it&#39;s not a top-level import</span>
        <span class="kn">from</span> <span class="nn">pymatgen.analysis.graphs</span> <span class="kn">import</span> <span class="n">StructureGraph</span>

        <span class="k">if</span> <span class="n">decorate</span><span class="p">:</span>
            <span class="c1"># Decorate all sites in the underlying structure</span>
            <span class="c1"># with site properties that provides information on the</span>
            <span class="c1"># coordination number and coordination pattern based</span>
            <span class="c1"># on the (current) structure of this graph.</span>
            <span class="n">order_parameters</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_local_order_parameters</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
                                <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">))]</span>
            <span class="n">structure</span><span class="o">.</span><span class="n">add_site_property</span><span class="p">(</span><span class="s1">&#39;order_parameters&#39;</span><span class="p">,</span> <span class="n">order_parameters</span><span class="p">)</span>

        <span class="n">sg</span> <span class="o">=</span> <span class="n">StructureGraph</span><span class="o">.</span><span class="n">with_local_env_strategy</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="n">weights</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">sg</span></div>

<div class="viewcode-block" id="NearNeighbors.get_local_order_parameters"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.NearNeighbors.get_local_order_parameters">[docs]</a>    <span class="k">def</span> <span class="nf">get_local_order_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate those local structure order parameters for</span>
<span class="sd">        the given site whose ideal CN corresponds to the</span>
<span class="sd">        underlying motif (e.g., CN=4, then calculate the</span>
<span class="sd">        square planar, tetrahedral, see-saw-like,</span>
<span class="sd">        rectangular see-saw-like order paramters).</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: Structure object</span>
<span class="sd">            n (int): site index.</span>

<span class="sd">        Returns (Dict[str, float]):</span>
<span class="sd">            A dict of order parameters (values) and the</span>
<span class="sd">            underlying motif type (keys; for example, tetrahedral).</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># code from @nisse3000, moved here from graphs to avoid circular</span>
        <span class="c1"># import, also makes sense to have this as a general NN method</span>
        <span class="n">cn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cn</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">cn</span> <span class="ow">in</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">k_cn</span><span class="p">)</span> <span class="k">for</span> <span class="n">k_cn</span> <span class="ow">in</span> <span class="n">cn_opt_params</span><span class="o">.</span><span class="n">keys</span><span class="p">()]:</span>
            <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">cn_opt_params</span><span class="p">[</span><span class="n">cn</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()]</span>
            <span class="n">types</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
                <span class="n">types</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cn_opt_params</span><span class="p">[</span><span class="n">cn</span><span class="p">][</span><span class="n">name</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
                <span class="n">tmp</span> <span class="o">=</span> <span class="n">cn_opt_params</span><span class="p">[</span><span class="n">cn</span><span class="p">][</span><span class="n">name</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> \
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cn_opt_params</span><span class="p">[</span><span class="n">cn</span><span class="p">][</span><span class="n">name</span><span class="p">])</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="n">params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
            <span class="n">lostops</span> <span class="o">=</span> <span class="n">LocalStructOrderParams</span><span class="p">(</span><span class="n">types</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
            <span class="n">sites</span> <span class="o">=</span> <span class="p">[</span><span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nn</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
            <span class="n">lostop_vals</span> <span class="o">=</span> <span class="n">lostops</span><span class="o">.</span><span class="n">get_order_parameters</span><span class="p">(</span>
                <span class="n">sites</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">indices_neighs</span><span class="o">=</span><span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">cn</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)])</span>
            <span class="n">d</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">lostop</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lostop_vals</span><span class="p">):</span>
                <span class="n">d</span><span class="p">[</span><span class="n">names</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="n">lostop</span>
            <span class="k">return</span> <span class="n">d</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span></div></div>


<div class="viewcode-block" id="VoronoiNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.VoronoiNN">[docs]</a><span class="k">class</span> <span class="nc">VoronoiNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Uses a Voronoi algorithm to determine near neighbors for each site in a</span>
<span class="sd">    structure.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">targets</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">13.0</span><span class="p">,</span>
                 <span class="n">allow_pathological</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="s1">&#39;solid_angle&#39;</span><span class="p">,</span>
                 <span class="n">extra_nn_info</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">compute_adj_neighbors</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for near-neighbor finding. Faces that are smaller</span>
<span class="sd">                than `tol` fraction of the largest face are not included in the tessellation.</span>
<span class="sd">                (default: 0).</span>
<span class="sd">            targets (Element or list of Elements): target element(s).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for near-neighbor</span>
<span class="sd">                atoms. Defaults to 13.0.</span>
<span class="sd">            allow_pathological (bool): whether to allow infinite vertices in</span>
<span class="sd">                determination of Voronoi coordination.</span>
<span class="sd">            weight (string) - Statistic used to weigh neighbors (see the statistics</span>
<span class="sd">                available in get_voronoi_polyhedra)</span>
<span class="sd">            extra_nn_info (bool) - Add all polyhedron info to `get_nn_info`</span>
<span class="sd">            compute_adj_neighbors (bool) - Whether to compute which neighbors are adjacent. Turn off</span>
<span class="sd">                for faster performance</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">allow_pathological</span> <span class="o">=</span> <span class="n">allow_pathological</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">targets</span> <span class="o">=</span> <span class="n">targets</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">weight</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">extra_nn_info</span> <span class="o">=</span> <span class="n">extra_nn_info</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">compute_adj_neighbors</span> <span class="o">=</span> <span class="n">compute_adj_neighbors</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="VoronoiNN.get_voronoi_polyhedra"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.VoronoiNN.get_voronoi_polyhedra">[docs]</a>    <span class="k">def</span> <span class="nf">get_voronoi_polyhedra</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gives a weighted polyhedra around a site.</span>

<span class="sd">        See ref: A Proposed Rigorous Definition of Coordination Number,</span>
<span class="sd">        M. O&#39;Keeffe, Acta Cryst. (1979). A35, 772-775</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): structure for which to evaluate the</span>
<span class="sd">                coordination environment.</span>
<span class="sd">            n (integer): site index.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dict of sites sharing a common Voronoi facet with the site</span>
<span class="sd">            n mapped to a directory containing statistics about the facet:</span>
<span class="sd">                - solid_angle - Solid angle subtended by face</span>
<span class="sd">                - angle_normalized - Solid angle normalized such that the</span>
<span class="sd">                    faces with the largest</span>
<span class="sd">                - area - Area of the facet</span>
<span class="sd">                - face_dist - Distance between site n and the facet</span>
<span class="sd">                - volume - Volume of Voronoi cell for this face</span>
<span class="sd">                - n_verts - Number of vertices on the facet</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Assemble the list of neighbors used in the tessellation</span>
        <span class="c1">#   Gets all atoms within a certain radius</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">targets</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">targets</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">targets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">targets</span>
        <span class="n">center</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>

        <span class="n">cutoff</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span>

        <span class="c1"># max cutoff is the longest diagonal of the cell + room for noise</span>
        <span class="n">corners</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
        <span class="n">d_corners</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
                     <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">corners</span><span class="p">]</span>
        <span class="n">max_cutoff</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">d_corners</span><span class="p">)</span> <span class="o">+</span> <span class="mf">0.01</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">neighbors</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_sites_in_sphere</span><span class="p">(</span>
                    <span class="n">center</span><span class="o">.</span><span class="n">coords</span><span class="p">,</span> <span class="n">cutoff</span><span class="p">)</span>
                <span class="n">neighbors</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span>
                             <span class="nb">sorted</span><span class="p">(</span><span class="n">neighbors</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>

                <span class="c1"># Run the Voronoi tessellation</span>
                <span class="n">qvoronoi_input</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">coords</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">neighbors</span><span class="p">]</span>

                <span class="n">voro</span> <span class="o">=</span> <span class="n">Voronoi</span><span class="p">(</span>
                    <span class="n">qvoronoi_input</span><span class="p">)</span>  <span class="c1"># can give seg fault if cutoff is too small</span>

                <span class="c1"># Extract data about the site in question</span>
                <span class="n">cell_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_cell_info</span><span class="p">(</span>
                    <span class="n">structure</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">neighbors</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">voro</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">compute_adj_neighbors</span><span class="p">)</span>
                <span class="k">break</span>

            <span class="k">except</span> <span class="ne">RuntimeError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">cutoff</span> <span class="o">&gt;=</span> <span class="n">max_cutoff</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">args</span> <span class="ow">and</span> <span class="s2">&quot;vertex&quot;</span> <span class="ow">in</span> <span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
                        <span class="c1"># pass through the error raised by _extract_cell_info</span>
                        <span class="k">raise</span> <span class="n">e</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Error in Voronoi neighbor finding; &quot;</span>
                                           <span class="s2">&quot;max cutoff exceeded&quot;</span><span class="p">)</span>
                <span class="n">cutoff</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">cutoff</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">max_cutoff</span> <span class="o">+</span> <span class="mf">0.001</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">cell_info</span></div>

<div class="viewcode-block" id="VoronoiNN.get_all_voronoi_polyhedra"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.VoronoiNN.get_all_voronoi_polyhedra">[docs]</a>    <span class="k">def</span> <span class="nf">get_all_voronoi_polyhedra</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get the Voronoi polyhedra for all site in a simulation cell</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Structure to be evaluated</span>
<span class="sd">        Returns:</span>
<span class="sd">            A dict of sites sharing a common Voronoi facet with the site</span>
<span class="sd">            n mapped to a directory containing statistics about the facet:</span>
<span class="sd">                - solid_angle - Solid angle subtended by face</span>
<span class="sd">                - angle_normalized - Solid angle normalized such that the</span>
<span class="sd">                    faces with the largest</span>
<span class="sd">                - area - Area of the facet</span>
<span class="sd">                - face_dist - Distance between site n and the facet</span>
<span class="sd">                - volume - Volume of Voronoi cell for this face</span>
<span class="sd">                - n_verts - Number of vertices on the facet</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Special case: For atoms with 1 site, the atom in the root image is not included</span>
        <span class="c1">#   in the get_all_neighbors output. Rather than creating logic to add that atom</span>
        <span class="c1">#   to the neighbor list, which requires detecting whether it will be translated</span>
        <span class="c1">#   to reside within the unit cell before neighbor detection, it is less complex</span>
        <span class="c1">#   to just call the one-by-one operation</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_voronoi_polyhedra</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]</span>

        <span class="c1"># Assemble the list of neighbors used in the tessellation</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">targets</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">targets</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">targets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">targets</span>

        <span class="c1"># Initialize the list of sites with the atoms in the origin unit cell</span>
        <span class="c1">#  The `get_all_neighbors` function returns neighbors for each site&#39;s image in the</span>
        <span class="c1">#   original unit cell. We start off with these central atoms to ensure they are</span>
        <span class="c1">#   included in the tessellation</span>

        <span class="n">sites</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">to_unit_cell</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">structure</span><span class="p">]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">structure</span><span class="p">)]</span>

        <span class="c1"># Get all neighbors within a certain cutoff</span>
        <span class="c1">#   Record both the list of these neighbors, and the site indices</span>
        <span class="n">all_neighs</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_all_neighbors</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">,</span>
                                                 <span class="n">include_index</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                                 <span class="n">include_image</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">neighs</span> <span class="ow">in</span> <span class="n">all_neighs</span><span class="p">:</span>
            <span class="n">sites</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">neighs</span><span class="p">])</span>
            <span class="n">indices</span><span class="o">.</span><span class="n">extend</span><span class="p">([(</span><span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">],)</span> <span class="o">+</span> <span class="n">x</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">neighs</span><span class="p">])</span>

        <span class="c1"># Get the non-duplicates (using the site indices for performance/numerical stability)</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">indices</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
        <span class="n">indices</span><span class="p">,</span> <span class="n">uniq_inds</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="n">indices</span><span class="p">,</span> <span class="n">return_index</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">sites</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">sites</span><span class="p">)[</span><span class="n">uniq_inds</span><span class="p">]</span>

        <span class="c1"># Sort array such that atoms in the root image are first</span>
        <span class="c1">#   Exploit the fact that the array is sorted by the unique operation such that</span>
        <span class="c1">#   the images associated with atom 0 are first, followed by atom 1, etc.</span>
        <span class="n">root_images</span><span class="p">,</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">indices</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">:])</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>

        <span class="k">del</span> <span class="n">indices</span>  <span class="c1"># Save memory (tessellations can be costly)</span>

        <span class="c1"># Run the tessellation</span>
        <span class="n">qvoronoi_input</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">coords</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">sites</span><span class="p">]</span>
        <span class="n">voro</span> <span class="o">=</span> <span class="n">Voronoi</span><span class="p">(</span><span class="n">qvoronoi_input</span><span class="p">)</span>

        <span class="c1"># Get the information for each neighbor</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_extract_cell_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">sites</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span>
                                        <span class="n">voro</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">compute_adj_neighbors</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">root_images</span><span class="o">.</span><span class="n">tolist</span><span class="p">()]</span></div>

    <span class="k">def</span> <span class="nf">_get_elements</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">site</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the list of elements for a Site</span>

<span class="sd">        Args:</span>
<span class="sd">             site (Site): Site to assess</span>
<span class="sd">        Returns:</span>
<span class="sd">            [Element]: List of elements</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">,</span> <span class="n">Element</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">[</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">]</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">Element</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">)]</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">site</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">elements</span>

    <span class="k">def</span> <span class="nf">_is_in_targets</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">site</span><span class="p">,</span> <span class="n">targets</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Test whether a site contains elements in the target list</span>

<span class="sd">        Args:</span>
<span class="sd">            site (Site): Site to assess</span>
<span class="sd">            targets ([Element]) List of elements</span>
<span class="sd">        Returns:</span>
<span class="sd">             (boolean) Whether this site contains a certain list of elements</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">elems</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_elements</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">elems</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">elem</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">targets</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_extract_cell_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">sites</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">voro</span><span class="p">,</span>
                           <span class="n">compute_adj_neighbors</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get the information about a certain atom from the results of a tessellation</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure) - Structure being assessed</span>
<span class="sd">            site_idx (int) - Index of the atom in question</span>
<span class="sd">            sites ([Site]) - List of all sites in the tessellation</span>
<span class="sd">            targets ([Element]) - Target elements</span>
<span class="sd">            voro - Output of qvoronoi</span>
<span class="sd">            compute_adj_neighbors (boolean) - Whether to compute which neighbors are adjacent</span>
<span class="sd">        Returns:</span>
<span class="sd">            A dict of sites sharing a common Voronoi facet. Key is facet id</span>
<span class="sd">             (not useful) and values are dictionaries containing statistics</span>
<span class="sd">             about the facet:</span>
<span class="sd">                - site: Pymatgen site</span>
<span class="sd">                - solid_angle - Solid angle subtended by face</span>
<span class="sd">                - angle_normalized - Solid angle normalized such that the</span>
<span class="sd">                    faces with the largest</span>
<span class="sd">                - area - Area of the facet</span>
<span class="sd">                - face_dist - Distance between site n and the facet</span>
<span class="sd">                - volume - Volume of Voronoi cell for this face</span>
<span class="sd">                - n_verts - Number of vertices on the facet</span>
<span class="sd">                - adj_neighbors - Facet id&#39;s for the adjacent neighbors</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the coordinates of every vertex</span>
        <span class="n">all_vertices</span> <span class="o">=</span> <span class="n">voro</span><span class="o">.</span><span class="n">vertices</span>

        <span class="c1"># Get the coordinates of the central site</span>
        <span class="n">center_coords</span> <span class="o">=</span> <span class="n">sites</span><span class="p">[</span><span class="n">site_idx</span><span class="p">]</span><span class="o">.</span><span class="n">coords</span>

        <span class="c1"># Iterate through all the faces in the tessellation</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">nn</span><span class="p">,</span> <span class="n">vind</span> <span class="ow">in</span> <span class="n">voro</span><span class="o">.</span><span class="n">ridge_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># Get only those that include the site in question</span>
            <span class="k">if</span> <span class="n">site_idx</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
                <span class="n">other_site</span> <span class="o">=</span> <span class="n">nn</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">nn</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">site_idx</span> <span class="k">else</span> <span class="n">nn</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="k">if</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">in</span> <span class="n">vind</span><span class="p">:</span>
                    <span class="c1"># -1 indices correspond to the Voronoi cell</span>
                    <span class="c1">#  missing a face</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_pathological</span><span class="p">:</span>
                        <span class="k">continue</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;This structure is pathological,&quot;</span>
                                           <span class="s2">&quot; infinite vertex in the voronoi &quot;</span>
                                           <span class="s2">&quot;construction&quot;</span><span class="p">)</span>

                <span class="c1"># Get the solid angle of the face</span>
                <span class="n">facets</span> <span class="o">=</span> <span class="p">[</span><span class="n">all_vertices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">vind</span><span class="p">]</span>
                <span class="n">angle</span> <span class="o">=</span> <span class="n">solid_angle</span><span class="p">(</span><span class="n">center_coords</span><span class="p">,</span> <span class="n">facets</span><span class="p">)</span>

                <span class="c1"># Compute the volume of associated with this face</span>
                <span class="n">volume</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="c1">#  qvoronoi returns vertices in CCW order, so I can break</span>
                <span class="c1">#   the face up in to segments (0,1,2), (0,2,3), ... to compute</span>
                <span class="c1">#   its area where each number is a vertex size</span>
                <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">vind</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">vind</span><span class="p">[</span><span class="mi">2</span><span class="p">:]):</span>
                    <span class="n">volume</span> <span class="o">+=</span> <span class="n">vol_tetra</span><span class="p">(</span><span class="n">center_coords</span><span class="p">,</span>
                                        <span class="n">all_vertices</span><span class="p">[</span><span class="n">vind</span><span class="p">[</span><span class="mi">0</span><span class="p">]],</span>
                                        <span class="n">all_vertices</span><span class="p">[</span><span class="n">j</span><span class="p">],</span>
                                        <span class="n">all_vertices</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>

                <span class="c1"># Compute the distance of the site to the face</span>
                <span class="n">face_dist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span>
                    <span class="n">center_coords</span> <span class="o">-</span> <span class="n">sites</span><span class="p">[</span><span class="n">other_site</span><span class="p">]</span><span class="o">.</span><span class="n">coords</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>

                <span class="c1"># Compute the area of the face (knowing V=Ad/3)</span>
                <span class="n">face_area</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">volume</span> <span class="o">/</span> <span class="n">face_dist</span>

                <span class="c1"># Compute the normal of the facet</span>
                <span class="n">normal</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">sites</span><span class="p">[</span><span class="n">other_site</span><span class="p">]</span><span class="o">.</span><span class="n">coords</span><span class="p">,</span> <span class="n">center_coords</span><span class="p">)</span>
                <span class="n">normal</span> <span class="o">/=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">normal</span><span class="p">)</span>

                <span class="c1"># Store by face index</span>
                <span class="n">results</span><span class="p">[</span><span class="n">other_site</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">sites</span><span class="p">[</span><span class="n">other_site</span><span class="p">],</span>
                    <span class="s1">&#39;normal&#39;</span><span class="p">:</span> <span class="n">normal</span><span class="p">,</span>
                    <span class="s1">&#39;solid_angle&#39;</span><span class="p">:</span> <span class="n">angle</span><span class="p">,</span>
                    <span class="s1">&#39;volume&#39;</span><span class="p">:</span> <span class="n">volume</span><span class="p">,</span>
                    <span class="s1">&#39;face_dist&#39;</span><span class="p">:</span> <span class="n">face_dist</span><span class="p">,</span>
                    <span class="s1">&#39;area&#39;</span><span class="p">:</span> <span class="n">face_area</span><span class="p">,</span>
                    <span class="s1">&#39;n_verts&#39;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">vind</span><span class="p">)</span>
                <span class="p">}</span>

                <span class="c1"># If we are computing which neighbors are adjacent, store the vertices</span>
                <span class="k">if</span> <span class="n">compute_adj_neighbors</span><span class="p">:</span>
                    <span class="n">results</span><span class="p">[</span><span class="n">other_site</span><span class="p">][</span><span class="s1">&#39;verts&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">vind</span>

        <span class="c1"># all sites should have atleast two connected ridges in periodic system</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">results</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;No Voronoi neighbours found for site - try increasing cutoff&quot;</span><span class="p">)</span>

        <span class="c1"># Get only target elements</span>
        <span class="n">resultweighted</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">nn_index</span><span class="p">,</span> <span class="n">nstats</span> <span class="ow">in</span> <span class="n">results</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># Check if this is a target site</span>
            <span class="n">nn</span> <span class="o">=</span> <span class="n">nstats</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">nn</span><span class="o">.</span><span class="n">is_ordered</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">nn</span><span class="o">.</span><span class="n">specie</span> <span class="ow">in</span> <span class="n">targets</span><span class="p">:</span>
                    <span class="n">resultweighted</span><span class="p">[</span><span class="n">nn_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">nstats</span>
            <span class="k">else</span><span class="p">:</span>  <span class="c1"># is nn site is disordered</span>
                <span class="k">for</span> <span class="n">disordered_sp</span> <span class="ow">in</span> <span class="n">nn</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">disordered_sp</span> <span class="ow">in</span> <span class="n">targets</span><span class="p">:</span>
                        <span class="n">resultweighted</span><span class="p">[</span><span class="n">nn_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">nstats</span>

        <span class="c1"># If desired, determine which neighbors are adjacent</span>
        <span class="k">if</span> <span class="n">compute_adj_neighbors</span><span class="p">:</span>
            <span class="c1"># Initialize storage for the adjacent neighbors</span>
            <span class="n">adj_neighbors</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="p">,</span> <span class="p">[])</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">resultweighted</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>

            <span class="c1"># Find the neighbors that are adjacent by finding those</span>
            <span class="c1">#  that contain exactly two vertices</span>
            <span class="k">for</span> <span class="n">a_ind</span><span class="p">,</span> <span class="n">a_nninfo</span> <span class="ow">in</span> <span class="n">resultweighted</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="c1"># Get the indices for this site</span>
                <span class="n">a_verts</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">a_nninfo</span><span class="p">[</span><span class="s1">&#39;verts&#39;</span><span class="p">])</span>

                <span class="c1"># Loop over all neighbors that have an index lower that this one</span>
                <span class="c1">#  The goal here is to exploit the fact that neighbor adjacency is symmetric</span>
                <span class="c1">#  (if A is adj to B, B is adj to A)</span>
                <span class="k">for</span> <span class="n">b_ind</span><span class="p">,</span> <span class="n">b_nninfo</span> <span class="ow">in</span> <span class="n">resultweighted</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">b_ind</span> <span class="o">&gt;</span> <span class="n">a_ind</span><span class="p">:</span>
                        <span class="k">continue</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">a_verts</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">b_nninfo</span><span class="p">[</span><span class="s1">&#39;verts&#39;</span><span class="p">]))</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                        <span class="n">adj_neighbors</span><span class="p">[</span><span class="n">a_ind</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">b_ind</span><span class="p">)</span>
                        <span class="n">adj_neighbors</span><span class="p">[</span><span class="n">b_ind</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a_ind</span><span class="p">)</span>

            <span class="c1"># Store the results in the nn_info</span>
            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">neighbors</span> <span class="ow">in</span> <span class="n">adj_neighbors</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">resultweighted</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="s1">&#39;adj_neighbors&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">neighbors</span>

        <span class="k">return</span> <span class="n">resultweighted</span>

<div class="viewcode-block" id="VoronoiNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.VoronoiNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n in structure</span>
<span class="sd">        using Voronoi decomposition.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                sites.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a coordinated site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Run the tessellation</span>
        <span class="n">nns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_voronoi_polyhedra</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>

        <span class="c1"># Extract the NN info</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">nns</span><span class="p">)</span></div>

<div class="viewcode-block" id="VoronoiNN.get_all_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.VoronoiNN.get_all_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_all_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>

<span class="sd">        Returns:</span>
<span class="sd">            All nn info for all sites.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_voro_cells</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_voronoi_polyhedra</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_extract_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">cell</span><span class="p">)</span> <span class="k">for</span> <span class="n">cell</span> <span class="ow">in</span>
                <span class="n">all_voro_cells</span><span class="p">]</span></div>

    <span class="k">def</span> <span class="nf">_extract_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">nns</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Given Voronoi NNs, extract the NN info in the form needed by NearestNeighbors</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Structure being evaluated</span>
<span class="sd">            nns ([dicts]): Nearest neighbor information for a structure</span>
<span class="sd">        Returns:</span>
<span class="sd">            (list of tuples (Site, array, float)): See nn_info</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Get the target information</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">targets</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">targets</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">targets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">targets</span>

        <span class="c1"># Extract the NN info</span>
        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">max_weight</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">nn</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">weight</span><span class="p">]</span> <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">nns</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
        <span class="k">for</span> <span class="n">nstats</span> <span class="ow">in</span> <span class="n">nns</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">site</span> <span class="o">=</span> <span class="n">nstats</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">nstats</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">weight</span><span class="p">]</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">*</span> <span class="n">max_weight</span> \
                    <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_in_targets</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">targets</span><span class="p">):</span>
                <span class="n">nn_info</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">site</span><span class="p">,</span>
                           <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">site</span><span class="p">),</span>
                           <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">nstats</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">weight</span><span class="p">]</span> <span class="o">/</span> <span class="n">max_weight</span><span class="p">,</span>
                           <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span>
                               <span class="n">structure</span><span class="p">,</span> <span class="n">site</span><span class="p">)}</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_nn_info</span><span class="p">:</span>
                    <span class="c1"># Add all the information about the site</span>
                    <span class="n">poly_info</span> <span class="o">=</span> <span class="n">nstats</span>
                    <span class="k">del</span> <span class="n">poly_info</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span>
                    <span class="n">nn_info</span><span class="p">[</span><span class="s1">&#39;poly_info&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">poly_info</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nn_info</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">siw</span></div>


<span class="nd">@deprecated</span><span class="p">(</span><span class="n">replacement</span><span class="o">=</span><span class="n">VoronoiNN</span><span class="p">,</span>
            <span class="n">message</span><span class="o">=</span><span class="s1">&#39;Use VoronoiNN instead, and set &quot;tol&quot; to 0.5&#39;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">VoronoiNN_modified</span><span class="p">(</span><span class="n">VoronoiNN</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Modified VoronoiNN that only considers neighbors with more than 50% of the maximum weight</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">targets</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">10.0</span><span class="p">,</span>
                 <span class="n">allow_pathological</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="s1">&#39;solid_angle&#39;</span><span class="p">,</span>
                 <span class="n">extra_nn_info</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            targets (Element or list of Elements): target element(s).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for near-neighbor</span>
<span class="sd">                atoms. Defaults to 10.0.</span>
<span class="sd">            allow_pathological (bool): whether to allow infinite vertices in</span>
<span class="sd">                determination of Voronoi coordination.</span>
<span class="sd">            weight (string) - Statistic used to weigh neighbors (see the statistics</span>
<span class="sd">                available in get_voronoi_polyhedra)</span>
<span class="sd">            extra_nn_info (bool) - Add all polyhedron info to `get_nn_info`</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">cutoff</span><span class="p">,</span> <span class="n">allow_pathological</span><span class="p">,</span> <span class="n">weight</span><span class="p">,</span>
                         <span class="n">extra_nn_info</span><span class="p">)</span>


<div class="viewcode-block" id="JmolNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.JmolNN">[docs]</a><span class="k">class</span> <span class="nc">JmolNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine near-neighbor sites and coordination number using an emulation</span>
<span class="sd">    of Jmol&#39;s default autoBond() algorithm. This version of the algorithm</span>
<span class="sd">    does not take into account any information regarding known charge</span>
<span class="sd">    states.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">0.56</span><span class="p">,</span> <span class="n">min_bond_distance</span><span class="o">=</span><span class="mf">0.4</span><span class="p">,</span> <span class="n">el_radius_updates</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for bond determination</span>
<span class="sd">                (default: 0.56).</span>
<span class="sd">            el_radius_updates: (dict) symbol-&gt;float to override default atomic</span>
<span class="sd">                radii table values</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_bond_distance</span> <span class="o">=</span> <span class="n">min_bond_distance</span>

        <span class="c1"># Load elemental radii table</span>
        <span class="n">bonds_file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)),</span>
                                  <span class="s2">&quot;bonds_jmol_ob.yaml&quot;</span><span class="p">)</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">bonds_file</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">el_radius</span> <span class="o">=</span> <span class="n">yaml</span><span class="o">.</span><span class="n">safe_load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

        <span class="c1"># Update any user preference elemental radii</span>
        <span class="k">if</span> <span class="n">el_radius_updates</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">el_radius</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">el_radius_updates</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

<div class="viewcode-block" id="JmolNN.get_max_bond_distance"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.JmolNN.get_max_bond_distance">[docs]</a>    <span class="k">def</span> <span class="nf">get_max_bond_distance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">el1_sym</span><span class="p">,</span> <span class="n">el2_sym</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Use Jmol algorithm to determine bond length from atomic parameters</span>
<span class="sd">        Args:</span>
<span class="sd">            el1_sym: (str) symbol of atom 1</span>
<span class="sd">            el2_sym: (str) symbol of atom 2</span>

<span class="sd">        Returns: (float) max bond length</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">sqrt</span><span class="p">(</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">el_radius</span><span class="p">[</span><span class="n">el1_sym</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">el_radius</span><span class="p">[</span><span class="n">el2_sym</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span></div>

<div class="viewcode-block" id="JmolNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.JmolNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n using the bond identification</span>
<span class="sd">        algorithm underlying Jmol.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near</span>
<span class="sd">                neighbors.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a neighbor site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>

        <span class="c1"># Determine relevant bond lengths based on atomic radii table</span>
        <span class="n">bonds</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
            <span class="n">bonds</span><span class="p">[</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">,</span> <span class="n">el</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_max_bond_distance</span><span class="p">(</span>
                <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span> <span class="n">el</span><span class="o">.</span><span class="n">symbol</span><span class="p">)</span>

        <span class="c1"># Search for neighbors up to max bond length + tolerance</span>
        <span class="n">max_rad</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">bonds</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span>
        <span class="n">min_rad</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">bonds</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">max_rad</span><span class="p">):</span>
            <span class="n">dist</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
            <span class="c1"># Confirm neighbor based on bond length specific to atom pair</span>
            <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;=</span> <span class="p">(</span><span class="n">bonds</span><span class="p">[(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">specie</span><span class="p">)])</span> <span class="ow">and</span> <span class="p">(</span>
                    <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_bond_distance</span><span class="p">):</span>
                <span class="n">weight</span> <span class="o">=</span> <span class="n">min_rad</span> <span class="o">/</span> <span class="n">dist</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">nn</span><span class="p">,</span>
                            <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">nn</span><span class="p">),</span>
                            <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">weight</span><span class="p">,</span>
                            <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                                  <span class="n">nn</span><span class="p">)})</span>
        <span class="k">return</span> <span class="n">siw</span></div></div>


<div class="viewcode-block" id="MinimumDistanceNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.MinimumDistanceNN">[docs]</a><span class="k">class</span> <span class="nc">MinimumDistanceNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine near-neighbor sites and coordination number using the</span>
<span class="sd">    nearest neighbor(s) at distance, d_min, plus all neighbors</span>
<span class="sd">    within a distance (1 + tol) * d_min, where tol is a</span>
<span class="sd">    (relative) distance tolerance parameter.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">10.0</span><span class="p">,</span> <span class="n">get_all_sites</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for neighbor identification</span>
<span class="sd">                (default: 0.1).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for trial</span>
<span class="sd">                near-neighbor sites (default: 10.0).</span>
<span class="sd">            get_all_sites (boolean): If this is set to True then the neighbor</span>
<span class="sd">                sites are only determined by the cutoff radius, tol is ignored</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">get_all_sites</span> <span class="o">=</span> <span class="n">get_all_sites</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

<div class="viewcode-block" id="MinimumDistanceNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.MinimumDistanceNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n using the closest neighbor</span>
<span class="sd">        distance-based method.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near</span>
<span class="sd">                neighbors.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a neighbor site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="n">neighs_dists</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">)</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_sites</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">nn</span><span class="p">,</span>
                            <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">nn</span><span class="p">),</span>
                            <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                            <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                                  <span class="n">nn</span><span class="p">)})</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">min_dist</span> <span class="o">=</span> <span class="nb">min</span><span class="p">([</span><span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span> <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
                <span class="n">dist</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
                <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">)</span> <span class="o">*</span> <span class="n">min_dist</span><span class="p">:</span>
                    <span class="n">w</span> <span class="o">=</span> <span class="n">min_dist</span> <span class="o">/</span> <span class="n">dist</span>
                    <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">nn</span><span class="p">,</span>
                                <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">nn</span><span class="p">),</span>
                                <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                                <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                                      <span class="n">nn</span><span class="p">)})</span>
        <span class="k">return</span> <span class="n">siw</span></div></div>


<div class="viewcode-block" id="OpenBabelNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.OpenBabelNN">[docs]</a><span class="k">class</span> <span class="nc">OpenBabelNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine near-neighbor sites and bond orders using OpenBabel API.</span>

<span class="sd">    NOTE: This strategy is only appropriate for molecules, and not for</span>
<span class="sd">    structures.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@requires</span><span class="p">(</span><span class="n">ob</span><span class="p">,</span>
              <span class="s2">&quot;BabelMolAdaptor requires openbabel to be installed with &quot;</span>
              <span class="s2">&quot;Python bindings. Please get it at http://openbabel.org &quot;</span>
              <span class="s2">&quot;(version &gt;=3.0.0).&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            order (bool): True if bond order should be returned as a weight, False</span>
<span class="sd">            if bond length should be used as a weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">order</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="OpenBabelNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.OpenBabelNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites and weights (orders) of bonds for a given</span>
<span class="sd">        atom.</span>

<span class="sd">        :param molecule: input Molecule.</span>
<span class="sd">        :param n: index of site for which to determine near neighbors.</span>
<span class="sd">        :return: [dict] representing a neighboring site and the type of</span>
<span class="sd">            bond present between site n and the neighboring site.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="kn">from</span> <span class="nn">pymatgen.io.babel</span> <span class="kn">import</span> <span class="n">BabelMolAdaptor</span>

        <span class="n">obmol</span> <span class="o">=</span> <span class="n">BabelMolAdaptor</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span><span class="o">.</span><span class="n">openbabel_mol</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Get only the atom of interest</span>
        <span class="n">site_atom</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ob</span><span class="o">.</span><span class="n">OBMolAtomDFSIter</span><span class="p">(</span><span class="n">obmol</span><span class="p">))</span>
                     <span class="k">if</span> <span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">GetX</span><span class="p">(),</span> <span class="n">a</span><span class="o">.</span><span class="n">GetY</span><span class="p">(),</span> <span class="n">a</span><span class="o">.</span><span class="n">GetZ</span><span class="p">()]</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span>
                <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">coords</span><span class="p">)][</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">neighbor</span> <span class="ow">in</span> <span class="n">ob</span><span class="o">.</span><span class="n">OBAtomAtomIter</span><span class="p">(</span><span class="n">site_atom</span><span class="p">):</span>
            <span class="n">coords</span> <span class="o">=</span> <span class="p">[</span><span class="n">neighbor</span><span class="o">.</span><span class="n">GetX</span><span class="p">(),</span> <span class="n">neighbor</span><span class="o">.</span><span class="n">GetY</span><span class="p">(),</span> <span class="n">neighbor</span><span class="o">.</span><span class="n">GetZ</span><span class="p">()]</span>
            <span class="n">site</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">structure</span> <span class="k">if</span> <span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">coords</span><span class="p">)</span> <span class="o">==</span> <span class="n">coords</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>

            <span class="n">bond</span> <span class="o">=</span> <span class="n">site_atom</span><span class="o">.</span><span class="n">GetBond</span><span class="p">(</span><span class="n">neighbor</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
                <span class="n">obmol</span><span class="o">.</span><span class="n">PerceiveBondOrders</span><span class="p">()</span>
                <span class="n">weight</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">GetBondOrder</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">weight</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">GetLength</span><span class="p">()</span>

            <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s2">&quot;site&quot;</span><span class="p">:</span> <span class="n">site</span><span class="p">,</span>
                        <span class="s2">&quot;image&quot;</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                        <span class="s2">&quot;weight&quot;</span><span class="p">:</span> <span class="n">weight</span><span class="p">,</span>
                        <span class="s2">&quot;site_index&quot;</span><span class="p">:</span> <span class="n">index</span><span class="p">})</span>

        <span class="k">return</span> <span class="n">siw</span></div>

<div class="viewcode-block" id="OpenBabelNN.get_bonded_structure"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.OpenBabelNN.get_bonded_structure">[docs]</a>    <span class="k">def</span> <span class="nf">get_bonded_structure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">decorate</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Obtain a MoleculeGraph object using this NearNeighbor</span>
<span class="sd">        class. Requires the optional dependency networkx</span>
<span class="sd">        (pip install networkx).</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: Molecule object.</span>
<span class="sd">            decorate (bool): whether to annotate site properties</span>
<span class="sd">            with order parameters using neighbors determined by</span>
<span class="sd">            this NearNeighbor class</span>

<span class="sd">        Returns: a pymatgen.analysis.graphs.MoleculeGraph object</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># requires optional dependency which is why it&#39;s not a top-level import</span>
        <span class="kn">from</span> <span class="nn">pymatgen.analysis.graphs</span> <span class="kn">import</span> <span class="n">MoleculeGraph</span>

        <span class="k">if</span> <span class="n">decorate</span><span class="p">:</span>
            <span class="c1"># Decorate all sites in the underlying structure</span>
            <span class="c1"># with site properties that provides information on the</span>
            <span class="c1"># coordination number and coordination pattern based</span>
            <span class="c1"># on the (current) structure of this graph.</span>
            <span class="n">order_parameters</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_local_order_parameters</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
                                <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">))]</span>
            <span class="n">structure</span><span class="o">.</span><span class="n">add_site_property</span><span class="p">(</span><span class="s1">&#39;order_parameters&#39;</span><span class="p">,</span> <span class="n">order_parameters</span><span class="p">)</span>

        <span class="n">mg</span> <span class="o">=</span> <span class="n">MoleculeGraph</span><span class="o">.</span><span class="n">with_local_env_strategy</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">mg</span></div>

<div class="viewcode-block" id="OpenBabelNN.get_nn_shell_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.OpenBabelNN.get_nn_shell_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_shell_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">shell</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get a certain nearest neighbor shell for a certain site.</span>

<span class="sd">        Determines all non-backtracking paths through the neighbor network</span>
<span class="sd">        computed by `get_nn_info`. The weight is determined by multiplying</span>
<span class="sd">        the weight of the neighbor at each hop through the network. For</span>
<span class="sd">        example, a 2nd-nearest-neighbor that has a weight of 1 from its</span>
<span class="sd">        1st-nearest-neighbor and weight 0.5 from the original site will</span>
<span class="sd">        be assigned a weight of 0.5.</span>

<span class="sd">        As this calculation may involve computing the nearest neighbors of</span>
<span class="sd">        atoms multiple times, the calculation starts by computing all of the</span>
<span class="sd">        neighbor info and then calling `_get_nn_shell_info`. If you are likely</span>
<span class="sd">        to call this method for more than one site, consider calling `get_all_nn`</span>
<span class="sd">        first and then calling this protected method yourself.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Molecule): Input structure</span>
<span class="sd">            site_idx (int): index of site for which to determine neighbor</span>
<span class="sd">                information.</span>
<span class="sd">            shell (int): Which neighbor shell to retrieve (1 == 1st NN shell)</span>
<span class="sd">        Returns:</span>
<span class="sd">            list of dictionaries. Each entry in the list is information about</span>
<span class="sd">                a certain neighbor in the structure, in the same format as</span>
<span class="sd">                `get_nn_info`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">all_nn_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="n">sites</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_nn_shell_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">all_nn_info</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">shell</span><span class="p">)</span>

        <span class="c1"># Update the site positions</span>
        <span class="c1">#   Did not do this during NN options because that can be slower</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">info</span> <span class="ow">in</span> <span class="n">sites</span><span class="p">:</span>
            <span class="n">orig_site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">info</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">]]</span>
            <span class="n">info</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Site</span><span class="p">(</span><span class="n">orig_site</span><span class="o">.</span><span class="n">species</span><span class="p">,</span>
                                <span class="n">orig_site</span><span class="o">.</span><span class="n">_coords</span><span class="p">,</span>
                                <span class="n">properties</span><span class="o">=</span><span class="n">orig_site</span><span class="o">.</span><span class="n">properties</span><span class="p">)</span>
            <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span></div></div>


<div class="viewcode-block" id="CovalentBondNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CovalentBondNN">[docs]</a><span class="k">class</span> <span class="nc">CovalentBondNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine near-neighbor sites and bond orders using built-in</span>
<span class="sd">    pymatgen.Molecule CovalentBond functionality.</span>

<span class="sd">    NOTE: This strategy is only appropriate for molecules, and not for</span>
<span class="sd">    structures.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): Tolerance for covalent bond checking.</span>
<span class="sd">            order (bool): If True (default), this class will compute bond</span>
<span class="sd">                orders. If False, bond lengths will be computed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">order</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">bonds</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="CovalentBondNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CovalentBondNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites and weights (orders) of bonds for a given</span>
<span class="sd">        atom.</span>

<span class="sd">        :param structure: input Molecule.</span>
<span class="sd">        :param n: index of site for which to determine near neighbors.</span>
<span class="sd">        :return: [dict] representing a neighboring site and the type of</span>
<span class="sd">            bond present between site n and the neighboring site.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># This is unfortunately inefficient, but is the best way to fit the</span>
        <span class="c1"># current NearNeighbors scheme</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bonds</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_covalent_bonds</span><span class="p">(</span><span class="n">tol</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">)</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">bond</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">bonds</span><span class="p">:</span>
            <span class="n">capture_bond</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">if</span> <span class="n">bond</span><span class="o">.</span><span class="n">site1</span> <span class="o">==</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]:</span>
                <span class="n">site</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">site2</span>
                <span class="n">capture_bond</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">elif</span> <span class="n">bond</span><span class="o">.</span><span class="n">site2</span> <span class="o">==</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]:</span>
                <span class="n">site</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">site1</span>
                <span class="n">capture_bond</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="k">if</span> <span class="n">capture_bond</span><span class="p">:</span>
                <span class="n">index</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
                    <span class="n">weight</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">get_bond_order</span><span class="p">()</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">weight</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">length</span>

                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s2">&quot;site&quot;</span><span class="p">:</span> <span class="n">site</span><span class="p">,</span>
                            <span class="s2">&quot;image&quot;</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                            <span class="s2">&quot;weight&quot;</span><span class="p">:</span> <span class="n">weight</span><span class="p">,</span>
                            <span class="s2">&quot;site_index&quot;</span><span class="p">:</span> <span class="n">index</span><span class="p">})</span>

        <span class="k">return</span> <span class="n">siw</span></div>

<div class="viewcode-block" id="CovalentBondNN.get_bonded_structure"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CovalentBondNN.get_bonded_structure">[docs]</a>    <span class="k">def</span> <span class="nf">get_bonded_structure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">decorate</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Obtain a MoleculeGraph object using this NearNeighbor</span>
<span class="sd">        class.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: Molecule object.</span>
<span class="sd">            decorate (bool): whether to annotate site properties</span>
<span class="sd">            with order parameters using neighbors determined by</span>
<span class="sd">            this NearNeighbor class</span>

<span class="sd">        Returns: a pymatgen.analysis.graphs.MoleculeGraph object</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># requires optional dependency which is why it&#39;s not a top-level import</span>
        <span class="kn">from</span> <span class="nn">pymatgen.analysis.graphs</span> <span class="kn">import</span> <span class="n">MoleculeGraph</span>

        <span class="k">if</span> <span class="n">decorate</span><span class="p">:</span>
            <span class="c1"># Decorate all sites in the underlying structure</span>
            <span class="c1"># with site properties that provides information on the</span>
            <span class="c1"># coordination number and coordination pattern based</span>
            <span class="c1"># on the (current) structure of this graph.</span>
            <span class="n">order_parameters</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_local_order_parameters</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
                                <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">))]</span>
            <span class="n">structure</span><span class="o">.</span><span class="n">add_site_property</span><span class="p">(</span><span class="s1">&#39;order_parameters&#39;</span><span class="p">,</span> <span class="n">order_parameters</span><span class="p">)</span>

        <span class="n">mg</span> <span class="o">=</span> <span class="n">MoleculeGraph</span><span class="o">.</span><span class="n">with_local_env_strategy</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">mg</span></div>

<div class="viewcode-block" id="CovalentBondNN.get_nn_shell_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CovalentBondNN.get_nn_shell_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_shell_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">shell</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get a certain nearest neighbor shell for a certain site.</span>

<span class="sd">        Determines all non-backtracking paths through the neighbor network</span>
<span class="sd">        computed by `get_nn_info`. The weight is determined by multiplying</span>
<span class="sd">        the weight of the neighbor at each hop through the network. For</span>
<span class="sd">        example, a 2nd-nearest-neighbor that has a weight of 1 from its</span>
<span class="sd">        1st-nearest-neighbor and weight 0.5 from the original site will</span>
<span class="sd">        be assigned a weight of 0.5.</span>

<span class="sd">        As this calculation may involve computing the nearest neighbors of</span>
<span class="sd">        atoms multiple times, the calculation starts by computing all of the</span>
<span class="sd">        neighbor info and then calling `_get_nn_shell_info`. If you are likely</span>
<span class="sd">        to call this method for more than one site, consider calling `get_all_nn`</span>
<span class="sd">        first and then calling this protected method yourself.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Molecule): Input structure</span>
<span class="sd">            site_idx (int): index of site for which to determine neighbor</span>
<span class="sd">                information.</span>
<span class="sd">            shell (int): Which neighbor shell to retrieve (1 == 1st NN shell)</span>
<span class="sd">        Returns:</span>
<span class="sd">            list of dictionaries. Each entry in the list is information about</span>
<span class="sd">                a certain neighbor in the structure, in the same format as</span>
<span class="sd">                `get_nn_info`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">all_nn_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="n">sites</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_nn_shell_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">all_nn_info</span><span class="p">,</span> <span class="n">site_idx</span><span class="p">,</span> <span class="n">shell</span><span class="p">)</span>

        <span class="c1"># Update the site positions</span>
        <span class="c1">#   Did not do this during NN options because that can be slower</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">info</span> <span class="ow">in</span> <span class="n">sites</span><span class="p">:</span>
            <span class="n">orig_site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">info</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">]]</span>
            <span class="n">info</span><span class="p">[</span><span class="s1">&#39;site&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Site</span><span class="p">(</span><span class="n">orig_site</span><span class="o">.</span><span class="n">species</span><span class="p">,</span>
                                <span class="n">orig_site</span><span class="o">.</span><span class="n">_coords</span><span class="p">,</span>
                                <span class="n">properties</span><span class="o">=</span><span class="n">orig_site</span><span class="o">.</span><span class="n">properties</span><span class="p">)</span>
            <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span></div></div>


<div class="viewcode-block" id="MinimumOKeeffeNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.MinimumOKeeffeNN">[docs]</a><span class="k">class</span> <span class="nc">MinimumOKeeffeNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine near-neighbor sites and coordination number using the</span>
<span class="sd">    neighbor(s) at closest relative distance, d_min_OKeffee, plus some</span>
<span class="sd">    relative tolerance, where bond valence parameters from O&#39;Keeffe&#39;s</span>
<span class="sd">    bond valence method (J. Am. Chem. Soc. 1991, 3226-3229) are used</span>
<span class="sd">    to calculate relative distances.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">10.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for neighbor identification</span>
<span class="sd">                (default: 0.1).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for trial</span>
<span class="sd">                near-neighbor sites (default: 10.0).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

<div class="viewcode-block" id="MinimumOKeeffeNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.MinimumOKeeffeNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n using the closest relative</span>
<span class="sd">        neighbor distance-based method with O&#39;Keeffe parameters.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near</span>
<span class="sd">                neighbors.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a neighbor site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="n">neighs_dists</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">eln</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">element</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="n">eln</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">species_string</span>

        <span class="n">reldists_neighs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
            <span class="n">neigh</span> <span class="o">=</span> <span class="n">nn</span>
            <span class="n">dist</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">el2</span> <span class="o">=</span> <span class="n">neigh</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">element</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="n">el2</span> <span class="o">=</span> <span class="n">neigh</span><span class="o">.</span><span class="n">species_string</span>
            <span class="n">reldists_neighs</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">dist</span> <span class="o">/</span> <span class="n">get_okeeffe_distance_prediction</span><span class="p">(</span>
                <span class="n">eln</span><span class="p">,</span> <span class="n">el2</span><span class="p">),</span> <span class="n">neigh</span><span class="p">])</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">min_reldist</span> <span class="o">=</span> <span class="nb">min</span><span class="p">([</span><span class="n">reldist</span> <span class="k">for</span> <span class="n">reldist</span><span class="p">,</span> <span class="n">neigh</span> <span class="ow">in</span> <span class="n">reldists_neighs</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">reldist</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">reldists_neighs</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">reldist</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">)</span> <span class="o">*</span> <span class="n">min_reldist</span><span class="p">:</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">min_reldist</span> <span class="o">/</span> <span class="n">reldist</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">s</span><span class="p">,</span>
                            <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">s</span><span class="p">),</span>
                            <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                            <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                                  <span class="n">s</span><span class="p">)})</span>

        <span class="k">return</span> <span class="n">siw</span></div></div>


<div class="viewcode-block" id="MinimumVIRENN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.MinimumVIRENN">[docs]</a><span class="k">class</span> <span class="nc">MinimumVIRENN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine near-neighbor sites and coordination number using the</span>
<span class="sd">    neighbor(s) at closest relative distance, d_min_VIRE, plus some</span>
<span class="sd">    relative tolerance, where atom radii from the</span>
<span class="sd">    ValenceIonicRadiusEvaluator (VIRE) are used</span>
<span class="sd">    to calculate relative distances.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">10.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for neighbor identification</span>
<span class="sd">                (default: 0.1).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for trial</span>
<span class="sd">                near-neighbor sites (default: 10.0).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="MinimumVIRENN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.MinimumVIRENN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n using the closest relative</span>
<span class="sd">        neighbor distance-based method with VIRE atomic/ionic radii.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near</span>
<span class="sd">                neighbors.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a neighbor site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">vire</span> <span class="o">=</span> <span class="n">_get_vire</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="n">site</span> <span class="o">=</span> <span class="n">vire</span><span class="o">.</span><span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="n">neighs_dists</span> <span class="o">=</span> <span class="n">vire</span><span class="o">.</span><span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">)</span>
        <span class="n">rn</span> <span class="o">=</span> <span class="n">vire</span><span class="o">.</span><span class="n">radii</span><span class="p">[</span><span class="n">vire</span><span class="o">.</span><span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">species_string</span><span class="p">]</span>

        <span class="n">reldists_neighs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
            <span class="n">reldists_neighs</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span> <span class="o">/</span> <span class="p">(</span>
                    <span class="n">vire</span><span class="o">.</span><span class="n">radii</span><span class="p">[</span><span class="n">nn</span><span class="o">.</span><span class="n">species_string</span><span class="p">]</span> <span class="o">+</span> <span class="n">rn</span><span class="p">),</span>
                                    <span class="n">nn</span><span class="p">])</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">min_reldist</span> <span class="o">=</span> <span class="nb">min</span><span class="p">([</span><span class="n">reldist</span> <span class="k">for</span> <span class="n">reldist</span><span class="p">,</span> <span class="n">neigh</span> <span class="ow">in</span> <span class="n">reldists_neighs</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">reldist</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">reldists_neighs</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">reldist</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">)</span> <span class="o">*</span> <span class="n">min_reldist</span><span class="p">:</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">min_reldist</span> <span class="o">/</span> <span class="n">reldist</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">s</span><span class="p">,</span>
                            <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">vire</span><span class="o">.</span><span class="n">structure</span><span class="p">,</span> <span class="n">s</span><span class="p">),</span>
                            <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                            <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span>
                                <span class="n">vire</span><span class="o">.</span><span class="n">structure</span><span class="p">,</span> <span class="n">s</span><span class="p">)})</span>

        <span class="k">return</span> <span class="n">siw</span></div></div>


<span class="k">def</span> <span class="nf">_get_vire</span><span class="p">(</span><span class="n">structure</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">Structure</span><span class="p">,</span> <span class="n">IStructure</span><span class="p">]):</span>
    <span class="sd">&quot;&quot;&quot;Get the ValenceIonicRadiusEvaluator object for an structure taking</span>
<span class="sd">    advantage of caching.</span>

<span class="sd">    Args:</span>
<span class="sd">        structure: A structure.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Output of `ValenceIonicRadiusEvaluator(structure)`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># pymatgen does not hash Structure objects, so we need</span>
    <span class="c1"># to cast from Structure to the immutable IStructure</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">Structure</span><span class="p">):</span>
        <span class="n">structure</span> <span class="o">=</span> <span class="n">IStructure</span><span class="o">.</span><span class="n">from_sites</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">_get_vire_istructure</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>


<span class="nd">@lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_get_vire_istructure</span><span class="p">(</span><span class="n">structure</span><span class="p">:</span> <span class="n">IStructure</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Get the ValenceIonicRadiusEvaluator object for an immutable structure</span>
<span class="sd">    taking advantage of caching.</span>

<span class="sd">    Args:</span>
<span class="sd">        structure: A structure.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Output of `ValenceIonicRadiusEvaluator(structure)`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">ValenceIonicRadiusEvaluator</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>


<div class="viewcode-block" id="solid_angle"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.solid_angle">[docs]</a><span class="k">def</span> <span class="nf">solid_angle</span><span class="p">(</span><span class="n">center</span><span class="p">,</span> <span class="n">coords</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Helper method to calculate the solid angle of a set of coords from the</span>
<span class="sd">    center.</span>

<span class="sd">    Args:</span>
<span class="sd">        center (3x1 array): Center to measure solid angle from.</span>
<span class="sd">        coords (Nx3 array): List of coords to determine solid angle.</span>

<span class="sd">    Returns:</span>
<span class="sd">        The solid angle.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Compute the displacement from the center</span>
    <span class="n">r</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">center</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coords</span><span class="p">]</span>

    <span class="c1"># Compute the magnitude of each vector</span>
    <span class="n">r_norm</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">r</span><span class="p">]</span>

    <span class="c1"># Compute the solid angle for each tetrahedron that makes up the facet</span>
    <span class="c1">#  Following: https://en.wikipedia.org/wiki/Solid_angle#Tetrahedron</span>
    <span class="n">angle</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">tp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">r</span><span class="p">[</span><span class="n">j</span><span class="p">])))</span>
        <span class="n">de</span> <span class="o">=</span> <span class="p">(</span><span class="n">r_norm</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">r_norm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">r_norm</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span>
              <span class="n">r_norm</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">+</span>
              <span class="n">r_norm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">r</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">+</span>
              <span class="n">r_norm</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">r</span><span class="p">[</span><span class="n">j</span><span class="p">]))</span>
        <span class="k">if</span> <span class="n">de</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">my_angle</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">pi</span> <span class="k">if</span> <span class="n">tp</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">pi</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">my_angle</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">tp</span> <span class="o">/</span> <span class="n">de</span><span class="p">)</span>
        <span class="n">angle</span> <span class="o">+=</span> <span class="p">(</span><span class="n">my_angle</span> <span class="k">if</span> <span class="n">my_angle</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="n">my_angle</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span>

    <span class="k">return</span> <span class="n">angle</span></div>


<div class="viewcode-block" id="vol_tetra"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.vol_tetra">[docs]</a><span class="k">def</span> <span class="nf">vol_tetra</span><span class="p">(</span><span class="n">vt1</span><span class="p">,</span> <span class="n">vt2</span><span class="p">,</span> <span class="n">vt3</span><span class="p">,</span> <span class="n">vt4</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculate the volume of a tetrahedron, given the four vertices of vt1,</span>
<span class="sd">    vt2, vt3 and vt4.</span>
<span class="sd">    Args:</span>
<span class="sd">        vt1 (array-like): coordinates of vertex 1.</span>
<span class="sd">        vt2 (array-like): coordinates of vertex 2.</span>
<span class="sd">        vt3 (array-like): coordinates of vertex 3.</span>
<span class="sd">        vt4 (array-like): coordinates of vertex 4.</span>
<span class="sd">    Returns:</span>
<span class="sd">        (float): volume of the tetrahedron.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">vol_tetra</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">((</span><span class="n">vt1</span> <span class="o">-</span> <span class="n">vt4</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">((</span><span class="n">vt2</span> <span class="o">-</span> <span class="n">vt4</span><span class="p">),</span> <span class="p">(</span><span class="n">vt3</span> <span class="o">-</span> <span class="n">vt4</span><span class="p">))))</span> <span class="o">/</span> <span class="mi">6</span>
    <span class="k">return</span> <span class="n">vol_tetra</span></div>


<div class="viewcode-block" id="get_okeeffe_params"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.get_okeeffe_params">[docs]</a><span class="k">def</span> <span class="nf">get_okeeffe_params</span><span class="p">(</span><span class="n">el_symbol</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the elemental parameters related to atom size and</span>
<span class="sd">    electronegativity which are used for estimating bond-valence</span>
<span class="sd">    parameters (bond length) of pairs of atoms on the basis of data</span>
<span class="sd">    provided in &#39;Atoms Sizes and Bond Lengths in Molecules and Crystals&#39;</span>
<span class="sd">    (O&#39;Keeffe &amp; Brese, 1991).</span>

<span class="sd">    Args:</span>
<span class="sd">        el_symbol (str): element symbol.</span>
<span class="sd">    Returns:</span>
<span class="sd">        (dict): atom-size (&#39;r&#39;) and electronegativity-related (&#39;c&#39;)</span>
<span class="sd">                parameter.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">el</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="n">el_symbol</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">el</span> <span class="ow">not</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">BV_PARAMS</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Could not find O&#39;Keeffe parameters for element&quot;</span>
                           <span class="s2">&quot; </span><span class="se">\&quot;</span><span class="si">{}</span><span class="se">\&quot;</span><span class="s2"> in </span><span class="se">\&quot;</span><span class="s2">BV_PARAMS</span><span class="se">\&quot;</span><span class="s2">dictonary&quot;</span>
                           <span class="s2">&quot; provided by pymatgen&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">el_symbol</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">BV_PARAMS</span><span class="p">[</span><span class="n">el</span><span class="p">]</span></div>


<div class="viewcode-block" id="get_okeeffe_distance_prediction"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.get_okeeffe_distance_prediction">[docs]</a><span class="k">def</span> <span class="nf">get_okeeffe_distance_prediction</span><span class="p">(</span><span class="n">el1</span><span class="p">,</span> <span class="n">el2</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns an estimate of the bond valence parameter (bond length) using</span>
<span class="sd">    the derived parameters from &#39;Atoms Sizes and Bond Lengths in Molecules</span>
<span class="sd">    and Crystals&#39; (O&#39;Keeffe &amp; Brese, 1991). The estimate is based on two</span>
<span class="sd">    experimental parameters: r and c. The value for r  is based off radius,</span>
<span class="sd">    while c is (usually) the Allred-Rochow electronegativity. Values used</span>
<span class="sd">    are *not* generated from pymatgen, and are found in</span>
<span class="sd">    &#39;okeeffe_params.json&#39;.</span>

<span class="sd">    Args:</span>
<span class="sd">        el1, el2 (Element): two Element objects</span>
<span class="sd">    Returns:</span>
<span class="sd">        a float value of the predicted bond length</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">el1_okeeffe_params</span> <span class="o">=</span> <span class="n">get_okeeffe_params</span><span class="p">(</span><span class="n">el1</span><span class="p">)</span>
    <span class="n">el2_okeeffe_params</span> <span class="o">=</span> <span class="n">get_okeeffe_params</span><span class="p">(</span><span class="n">el2</span><span class="p">)</span>

    <span class="n">r1</span> <span class="o">=</span> <span class="n">el1_okeeffe_params</span><span class="p">[</span><span class="s1">&#39;r&#39;</span><span class="p">]</span>
    <span class="n">r2</span> <span class="o">=</span> <span class="n">el2_okeeffe_params</span><span class="p">[</span><span class="s1">&#39;r&#39;</span><span class="p">]</span>
    <span class="n">c1</span> <span class="o">=</span> <span class="n">el1_okeeffe_params</span><span class="p">[</span><span class="s1">&#39;c&#39;</span><span class="p">]</span>
    <span class="n">c2</span> <span class="o">=</span> <span class="n">el2_okeeffe_params</span><span class="p">[</span><span class="s1">&#39;c&#39;</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">r1</span> <span class="o">+</span> <span class="n">r2</span> <span class="o">-</span> <span class="n">r1</span> <span class="o">*</span> <span class="n">r2</span> <span class="o">*</span> <span class="nb">pow</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c1</span><span class="p">)</span> <span class="o">-</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">c2</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">c1</span> <span class="o">*</span> <span class="n">r1</span> <span class="o">+</span> <span class="n">c2</span> <span class="o">*</span> <span class="n">r2</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_neighbors_of_site_with_index"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.get_neighbors_of_site_with_index">[docs]</a><span class="k">def</span> <span class="nf">get_neighbors_of_site_with_index</span><span class="p">(</span><span class="n">struct</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">approach</span><span class="o">=</span><span class="s2">&quot;min_dist&quot;</span><span class="p">,</span> <span class="n">delta</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">10.0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the neighbors of a given site using a specific neighbor-finding</span>
<span class="sd">    method.</span>

<span class="sd">    Args:</span>
<span class="sd">        struct (Structure): input structure.</span>
<span class="sd">        n (int): index of site in Structure object for which motif type</span>
<span class="sd">            is to be determined.</span>
<span class="sd">        approach (str): type of neighbor-finding approach, where</span>
<span class="sd">            &quot;min_dist&quot; will use the MinimumDistanceNN class,</span>
<span class="sd">            &quot;voronoi&quot; the VoronoiNN class, &quot;min_OKeeffe&quot; the</span>
<span class="sd">            MinimumOKeeffe class, and &quot;min_VIRE&quot; the MinimumVIRENN class.</span>
<span class="sd">        delta (float): tolerance involved in neighbor finding.</span>
<span class="sd">        cutoff (float): (large) radius to find tentative neighbors.</span>

<span class="sd">    Returns: neighbor sites.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">approach</span> <span class="o">==</span> <span class="s2">&quot;min_dist&quot;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">MinimumDistanceNN</span><span class="p">(</span><span class="n">tol</span><span class="o">=</span><span class="n">delta</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="n">cutoff</span><span class="p">)</span><span class="o">.</span><span class="n">get_nn</span><span class="p">(</span>
            <span class="n">struct</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">approach</span> <span class="o">==</span> <span class="s2">&quot;voronoi&quot;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">VoronoiNN</span><span class="p">(</span><span class="n">tol</span><span class="o">=</span><span class="n">delta</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="n">cutoff</span><span class="p">)</span><span class="o">.</span><span class="n">get_nn</span><span class="p">(</span>
            <span class="n">struct</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">approach</span> <span class="o">==</span> <span class="s2">&quot;min_OKeeffe&quot;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">MinimumOKeeffeNN</span><span class="p">(</span><span class="n">tol</span><span class="o">=</span><span class="n">delta</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="n">cutoff</span><span class="p">)</span><span class="o">.</span><span class="n">get_nn</span><span class="p">(</span>
            <span class="n">struct</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">approach</span> <span class="o">==</span> <span class="s2">&quot;min_VIRE&quot;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">MinimumVIRENN</span><span class="p">(</span><span class="n">tol</span><span class="o">=</span><span class="n">delta</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="n">cutoff</span><span class="p">)</span><span class="o">.</span><span class="n">get_nn</span><span class="p">(</span>
            <span class="n">struct</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;unsupported neighbor-finding method (</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">approach</span><span class="p">))</span></div>


<div class="viewcode-block" id="site_is_of_motif_type"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.site_is_of_motif_type">[docs]</a><span class="k">def</span> <span class="nf">site_is_of_motif_type</span><span class="p">(</span><span class="n">struct</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">approach</span><span class="o">=</span><span class="s2">&quot;min_dist&quot;</span><span class="p">,</span> <span class="n">delta</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">10.0</span><span class="p">,</span> <span class="n">thresh</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the motif type of the site with index n in structure struct;</span>
<span class="sd">    currently featuring &quot;tetrahedral&quot;, &quot;octahedral&quot;, &quot;bcc&quot;, and &quot;cp&quot;</span>
<span class="sd">    (close-packed: fcc and hcp) as well as &quot;square pyramidal&quot; and</span>
<span class="sd">    &quot;trigonal bipyramidal&quot;.  If the site is not recognized,</span>
<span class="sd">    &quot;unrecognized&quot; is returned.  If a site should be assigned to two</span>
<span class="sd">    different motifs, &quot;multiple assignments&quot; is returned.</span>

<span class="sd">    Args:</span>
<span class="sd">        struct (Structure): input structure.</span>
<span class="sd">        n (int): index of site in Structure object for which motif type</span>
<span class="sd">                is to be determined.</span>
<span class="sd">        approach (str): type of neighbor-finding approach, where</span>
<span class="sd">              &quot;min_dist&quot; will use the MinimumDistanceNN class,</span>
<span class="sd">              &quot;voronoi&quot; the VoronoiNN class, &quot;min_OKeeffe&quot; the</span>
<span class="sd">              MinimumOKeeffe class, and &quot;min_VIRE&quot; the MinimumVIRENN class.</span>
<span class="sd">        delta (float): tolerance involved in neighbor finding.</span>
<span class="sd">        cutoff (float): (large) radius to find tentative neighbors.</span>
<span class="sd">        thresh (dict): thresholds for motif criteria (currently, required</span>
<span class="sd">                keys and their default values are &quot;qtet&quot;: 0.5,</span>
<span class="sd">                &quot;qoct&quot;: 0.5, &quot;qbcc&quot;: 0.5, &quot;q6&quot;: 0.4).</span>

<span class="sd">    Returns: motif type (str).</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">thresh</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">thresh</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;qtet&quot;</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">,</span> <span class="s2">&quot;qoct&quot;</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">,</span> <span class="s2">&quot;qbcc&quot;</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">,</span> <span class="s2">&quot;q6&quot;</span><span class="p">:</span> <span class="mf">0.4</span><span class="p">,</span>
            <span class="s2">&quot;qtribipyr&quot;</span><span class="p">:</span> <span class="mf">0.8</span><span class="p">,</span> <span class="s2">&quot;qsqpyr&quot;</span><span class="p">:</span> <span class="mf">0.8</span><span class="p">}</span>

    <span class="n">ops</span> <span class="o">=</span> <span class="n">LocalStructOrderParams</span><span class="p">([</span>
        <span class="s2">&quot;cn&quot;</span><span class="p">,</span> <span class="s2">&quot;tet&quot;</span><span class="p">,</span> <span class="s2">&quot;oct&quot;</span><span class="p">,</span> <span class="s2">&quot;bcc&quot;</span><span class="p">,</span> <span class="s2">&quot;q6&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_bipyr&quot;</span><span class="p">])</span>

    <span class="n">neighs_cent</span> <span class="o">=</span> <span class="n">get_neighbors_of_site_with_index</span><span class="p">(</span>
        <span class="n">struct</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">approach</span><span class="o">=</span><span class="n">approach</span><span class="p">,</span> <span class="n">delta</span><span class="o">=</span><span class="n">delta</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="n">cutoff</span><span class="p">)</span>
    <span class="n">neighs_cent</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">sites</span><span class="p">[</span><span class="n">n</span><span class="p">])</span>
    <span class="n">opvals</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">get_order_parameters</span><span class="p">(</span>
        <span class="n">neighs_cent</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">neighs_cent</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">indices_neighs</span><span class="o">=</span><span class="p">[</span>
            <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">neighs_cent</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)])</span>
    <span class="n">cn</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">opvals</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span>
    <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;unrecognized&quot;</span>
    <span class="n">nmotif</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">if</span> <span class="n">cn</span> <span class="o">==</span> <span class="mi">4</span> <span class="ow">and</span> <span class="n">opvals</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;qtet&quot;</span><span class="p">]:</span>
        <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;tetrahedral&quot;</span>
        <span class="n">nmotif</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">cn</span> <span class="o">==</span> <span class="mi">5</span> <span class="ow">and</span> <span class="n">opvals</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;qsqpyr&quot;</span><span class="p">]:</span>
        <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;square pyramidal&quot;</span>
        <span class="n">nmotif</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">cn</span> <span class="o">==</span> <span class="mi">5</span> <span class="ow">and</span> <span class="n">opvals</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;qtribipyr&quot;</span><span class="p">]:</span>
        <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;trigonal bipyramidal&quot;</span>
        <span class="n">nmotif</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">cn</span> <span class="o">==</span> <span class="mi">6</span> <span class="ow">and</span> <span class="n">opvals</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;qoct&quot;</span><span class="p">]:</span>
        <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;octahedral&quot;</span>
        <span class="n">nmotif</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">cn</span> <span class="o">==</span> <span class="mi">8</span> <span class="ow">and</span> <span class="p">(</span><span class="n">opvals</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;qbcc&quot;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">opvals</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;qtet&quot;</span><span class="p">]):</span>
        <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;bcc&quot;</span>
        <span class="n">nmotif</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">cn</span> <span class="o">==</span> <span class="mi">12</span> <span class="ow">and</span> <span class="p">(</span><span class="n">opvals</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;q6&quot;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">opvals</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;q6&quot;</span><span class="p">]</span> <span class="ow">and</span>
                     <span class="n">opvals</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;q6&quot;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">opvals</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">thresh</span><span class="p">[</span><span class="s2">&quot;q6&quot;</span><span class="p">]):</span>
        <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;cp&quot;</span>
        <span class="n">nmotif</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">if</span> <span class="n">nmotif</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">motif_type</span> <span class="o">=</span> <span class="s2">&quot;multiple assignments&quot;</span>

    <span class="k">return</span> <span class="n">motif_type</span></div>


<div class="viewcode-block" id="gramschmidt"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.gramschmidt">[docs]</a><span class="k">def</span> <span class="nf">gramschmidt</span><span class="p">(</span><span class="n">vin</span><span class="p">,</span> <span class="n">uin</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns that part of the first input vector</span>
<span class="sd">    that is orthogonal to the second input vector.</span>
<span class="sd">    The output vector is not normalized.</span>

<span class="sd">    Args:</span>
<span class="sd">        vin (numpy array):</span>
<span class="sd">            first input vector</span>
<span class="sd">        uin (numpy array):</span>
<span class="sd">            second input vector</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">vin_uin</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inner</span><span class="p">(</span><span class="n">vin</span><span class="p">,</span> <span class="n">uin</span><span class="p">)</span>
    <span class="n">uin_uin</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inner</span><span class="p">(</span><span class="n">uin</span><span class="p">,</span> <span class="n">uin</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">uin_uin</span> <span class="o">&lt;=</span> <span class="mf">0.0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Zero or negative inner product!&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">vin</span> <span class="o">-</span> <span class="p">(</span><span class="n">vin_uin</span> <span class="o">/</span> <span class="n">uin_uin</span><span class="p">)</span> <span class="o">*</span> <span class="n">uin</span></div>


<div class="viewcode-block" id="LocalStructOrderParams"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams">[docs]</a><span class="k">class</span> <span class="nc">LocalStructOrderParams</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class permits the calculation of various types of local</span>
<span class="sd">    structure order parameters.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">__supported_types</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s2">&quot;cn&quot;</span><span class="p">,</span> <span class="s2">&quot;sgl_bd&quot;</span><span class="p">,</span> <span class="s2">&quot;bent&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;reg_tri&quot;</span><span class="p">,</span>
        <span class="s2">&quot;sq_plan&quot;</span><span class="p">,</span>
        <span class="s2">&quot;sq_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;sq&quot;</span><span class="p">,</span> <span class="s2">&quot;tet&quot;</span><span class="p">,</span> <span class="s2">&quot;tet_max&quot;</span><span class="p">,</span>
        <span class="s2">&quot;tri_pyr&quot;</span><span class="p">,</span>
        <span class="s2">&quot;sq_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr_legacy&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;oct&quot;</span><span class="p">,</span>
        <span class="s2">&quot;oct_legacy&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_bipyr&quot;</span><span class="p">,</span>
        <span class="s2">&quot;T&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct_max&quot;</span><span class="p">,</span> <span class="s2">&quot;see_saw_rect&quot;</span><span class="p">,</span> <span class="s2">&quot;bcc&quot;</span><span class="p">,</span> <span class="s2">&quot;q2&quot;</span><span class="p">,</span> <span class="s2">&quot;q4&quot;</span><span class="p">,</span> <span class="s2">&quot;q6&quot;</span><span class="p">,</span>
        <span class="s2">&quot;oct_max&quot;</span><span class="p">,</span>
        <span class="s2">&quot;hex_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_face_cap_trig_pris&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=-</span><span class="mf">10.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            types ([string]): list of strings representing the types of</span>
<span class="sd">                order parameters to be calculated. Note that multiple</span>
<span class="sd">                mentions of the same type may occur. Currently available</span>
<span class="sd">                types recognize following environments:</span>
<span class="sd">                  &quot;cn&quot;: simple coordination number---normalized</span>
<span class="sd">                        if desired;</span>
<span class="sd">                  &quot;sgl_bd&quot;: single bonds;</span>
<span class="sd">                  &quot;bent&quot;: bent (angular) coordinations</span>
<span class="sd">                          (Zimmermann &amp; Jain, in progress, 2017);</span>
<span class="sd">                  &quot;T&quot;: T-shape coordinations;</span>
<span class="sd">                  &quot;see_saw_rect&quot;: see saw-like coordinations;</span>
<span class="sd">                  &quot;tet&quot;: tetrahedra</span>
<span class="sd">                         (Zimmermann et al., submitted, 2017);</span>
<span class="sd">                  &quot;oct&quot;: octahedra</span>
<span class="sd">                         (Zimmermann et al., submitted, 2017);</span>
<span class="sd">                  &quot;bcc&quot;: body-centered cubic environments (Peters,</span>
<span class="sd">                         J. Chem. Phys., 131, 244103, 2009);</span>
<span class="sd">                  &quot;tri_plan&quot;: trigonal planar environments;</span>
<span class="sd">                  &quot;sq_plan&quot;: square planar environments;</span>
<span class="sd">                  &quot;pent_plan&quot;: pentagonal planar environments;</span>
<span class="sd">                  &quot;tri_pyr&quot;: trigonal pyramids (coordinated atom is in</span>
<span class="sd">                             the center of the basal plane);</span>
<span class="sd">                  &quot;sq_pyr&quot;: square pyramids;</span>
<span class="sd">                  &quot;pent_pyr&quot;: pentagonal pyramids;</span>
<span class="sd">                  &quot;hex_pyr&quot;: hexagonal pyramids;</span>
<span class="sd">                  &quot;tri_bipyr&quot;: trigonal bipyramids;</span>
<span class="sd">                  &quot;sq_bipyr&quot;: square bipyramids;</span>
<span class="sd">                  &quot;pent_bipyr&quot;: pentagonal bipyramids;</span>
<span class="sd">                  &quot;hex_bipyr&quot;: hexagonal bipyramids;</span>
<span class="sd">                  &quot;cuboct&quot;: cuboctahedra;</span>
<span class="sd">                  &quot;q2&quot;: motif-unspecific bond orientational order</span>
<span class="sd">                        parameter (BOOP) of weight l=2 (Steinhardt</span>
<span class="sd">                        et al., Phys. Rev. B, 28, 784-805, 1983);</span>
<span class="sd">                  &quot;q4&quot;: BOOP of weight l=4;</span>
<span class="sd">                  &quot;q6&quot;: BOOP of weight l=6.</span>
<span class="sd">                  &quot;reg_tri&quot;: regular triangle with varying height</span>
<span class="sd">                             to basal plane;</span>
<span class="sd">                  &quot;sq&quot;: square coordination (cf., &quot;reg_tri&quot;);</span>
<span class="sd">                  &quot;oct_legacy&quot;: original Peters-style OP recognizing</span>
<span class="sd">                                octahedral coordination environments</span>
<span class="sd">                                (Zimmermann et al., J. Am. Chem. Soc.,</span>
<span class="sd">                                137, 13352-13361, 2015) that can, however,</span>
<span class="sd">                                produce small negative values sometimes.</span>
<span class="sd">                  &quot;sq_pyr_legacy&quot;: square pyramids (legacy);</span>
<span class="sd">            parameters ([dict]): list of dictionaries</span>
<span class="sd">                that store float-type parameters associated with the</span>
<span class="sd">                definitions of the different order parameters</span>
<span class="sd">                (length of list = number of OPs). If an entry</span>
<span class="sd">                is None, default values are used that are read from</span>
<span class="sd">                the op_params.yaml file. With few exceptions, 9 different</span>
<span class="sd">                parameters are used across all OPs:</span>
<span class="sd">                  &quot;norm&quot;: normalizing constant (used in &quot;cn&quot;</span>
<span class="sd">                      (default value: 1)).</span>
<span class="sd">                  &quot;TA&quot;: target angle (TA) in fraction of 180 degrees</span>
<span class="sd">                      (&quot;bent&quot; (1), &quot;tet&quot; (0.6081734479693927),</span>
<span class="sd">                      &quot;tri_plan&quot; (0.66666666667), &quot;pent_plan&quot; (0.6),</span>
<span class="sd">                      &quot;sq_pyr_legacy&quot; (0.5)).</span>
<span class="sd">                  &quot;IGW_TA&quot;: inverse Gaussian width (IGW) for penalizing</span>
<span class="sd">                      angles away from the target angle in inverse</span>
<span class="sd">                      fractions of 180 degrees to (&quot;bent&quot; and &quot;tet&quot; (15),</span>
<span class="sd">                      &quot;tri_plan&quot; (13.5), &quot;pent_plan&quot; (18),</span>
<span class="sd">                      &quot;sq_pyr_legacy&quot; (30)).</span>
<span class="sd">                  &quot;IGW_EP&quot;: IGW for penalizing angles away from the</span>
<span class="sd">                      equatorial plane (EP) at 90 degrees (&quot;T&quot;, &quot;see_saw_rect&quot;,</span>
<span class="sd">                      &quot;oct&quot;, &quot;sq_plan&quot;, &quot;tri_pyr&quot;, &quot;sq_pyr&quot;, &quot;pent_pyr&quot;,</span>
<span class="sd">                      &quot;hex_pyr&quot;, &quot;tri_bipyr&quot;, &quot;sq_bipyr&quot;, &quot;pent_bipyr&quot;,</span>
<span class="sd">                      &quot;hex_bipyr&quot;, and &quot;oct_legacy&quot; (18)).</span>
<span class="sd">                  &quot;fac_AA&quot;: factor applied to azimuth angle (AA) in cosine</span>
<span class="sd">                      term (&quot;T&quot;, &quot;tri_plan&quot;, and &quot;sq_plan&quot; (1), &quot;tet&quot;,</span>
<span class="sd">                      &quot;tri_pyr&quot;, and &quot;tri_bipyr&quot; (1.5), &quot;oct&quot;, &quot;sq_pyr&quot;,</span>
<span class="sd">                      &quot;sq_bipyr&quot;, and &quot;oct_legacy&quot; (2), &quot;pent_pyr&quot;</span>
<span class="sd">                      and &quot;pent_bipyr&quot; (2.5), &quot;hex_pyr&quot; and</span>
<span class="sd">                      &quot;hex_bipyr&quot; (3)).</span>
<span class="sd">                  &quot;exp_cos_AA&quot;: exponent applied to cosine term of AA</span>
<span class="sd">                      (&quot;T&quot;, &quot;tet&quot;, &quot;oct&quot;, &quot;tri_plan&quot;, &quot;sq_plan&quot;,</span>
<span class="sd">                      &quot;tri_pyr&quot;, &quot;sq_pyr&quot;, &quot;pent_pyr&quot;, &quot;hex_pyr&quot;,</span>
<span class="sd">                      &quot;tri_bipyr&quot;, &quot;sq_bipyr&quot;, &quot;pent_bipyr&quot;, &quot;hex_bipyr&quot;,</span>
<span class="sd">                      and &quot;oct_legacy&quot; (2)).</span>
<span class="sd">                  &quot;min_SPP&quot;: smallest angle (in radians) to consider</span>
<span class="sd">                      a neighbor to be</span>
<span class="sd">                      at South pole position (&quot;see_saw_rect&quot;, &quot;oct&quot;, &quot;bcc&quot;,</span>
<span class="sd">                      &quot;sq_plan&quot;, &quot;tri_bipyr&quot;, &quot;sq_bipyr&quot;, &quot;pent_bipyr&quot;,</span>
<span class="sd">                      &quot;hex_bipyr&quot;, &quot;cuboct&quot;, and &quot;oct_legacy&quot;</span>
<span class="sd">                      (2.792526803190927)).</span>
<span class="sd">                  &quot;IGW_SPP&quot;: IGW for penalizing angles away from South</span>
<span class="sd">                      pole position (&quot;see_saw_rect&quot;, &quot;oct&quot;, &quot;bcc&quot;, &quot;sq_plan&quot;,</span>
<span class="sd">                      &quot;tri_bipyr&quot;, &quot;sq_bipyr&quot;, &quot;pent_bipyr&quot;, &quot;hex_bipyr&quot;,</span>
<span class="sd">                      &quot;cuboct&quot;, and &quot;oct_legacy&quot; (15)).</span>
<span class="sd">                  &quot;w_SPP&quot;: weight for South pole position relative to</span>
<span class="sd">                      equatorial positions (&quot;see_saw_rect&quot; and &quot;sq_plan&quot; (1),</span>
<span class="sd">                      &quot;cuboct&quot; (1.8), &quot;tri_bipyr&quot; (2), &quot;oct&quot;,</span>
<span class="sd">                      &quot;sq_bipyr&quot;, and &quot;oct_legacy&quot; (3), &quot;pent_bipyr&quot; (4),</span>
<span class="sd">                      &quot;hex_bipyr&quot; (5), &quot;bcc&quot; (6)).</span>
<span class="sd">            cutoff (float): Cutoff radius to determine which nearest</span>
<span class="sd">                neighbors are supposed to contribute to the order</span>
<span class="sd">                parameters. If the value is negative the neighboring</span>
<span class="sd">                sites found by distance and cutoff radius are further</span>
<span class="sd">                pruned using the get_nn method from the</span>
<span class="sd">                VoronoiNN class.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">types</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">t</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">LocalStructOrderParams</span><span class="o">.</span><span class="n">__supported_types</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unknown order parameter type (&quot;</span> <span class="o">+</span> <span class="n">t</span> <span class="o">+</span> <span class="s2">&quot;)!&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_types</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_comp_azi</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
            <span class="n">d</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">default_op_params</span><span class="p">[</span><span class="n">t</span><span class="p">])</span> <span class="k">if</span> <span class="n">default_op_params</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">parameters</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">parameters</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">parameters</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_computerijs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_computerjks</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_geomops</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_geomops2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_boops</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_max_trig_order</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>

        <span class="c1"># Add here any additional flags to be used during calculation.</span>
        <span class="k">if</span> <span class="s2">&quot;sgl_bd&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_computerijs</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">)</span><span class="o">.</span><span class="n">isdisjoint</span><span class="p">(</span>
                <span class="p">[</span><span class="s2">&quot;tet&quot;</span><span class="p">,</span> <span class="s2">&quot;oct&quot;</span><span class="p">,</span> <span class="s2">&quot;bcc&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr_legacy&quot;</span><span class="p">,</span>
                 <span class="s2">&quot;tri_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_legacy&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_plan&quot;</span><span class="p">,</span>
                 <span class="s2">&quot;sq_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_pyr&quot;</span><span class="p">,</span>
                 <span class="s2">&quot;pent_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;T&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_max&quot;</span><span class="p">,</span> <span class="s2">&quot;tet_max&quot;</span><span class="p">,</span>
                 <span class="s2">&quot;tri_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct_max&quot;</span><span class="p">,</span>
                 <span class="s2">&quot;bent&quot;</span><span class="p">,</span> <span class="s2">&quot;see_saw_rect&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_plan_max&quot;</span><span class="p">,</span>
                 <span class="s2">&quot;sq_face_cap_trig_pris&quot;</span><span class="p">]):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_computerijs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_geomops</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="s2">&quot;sq_face_cap_trig_pris&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_comp_azi</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">)</span><span class="o">.</span><span class="n">isdisjoint</span><span class="p">([</span><span class="s2">&quot;reg_tri&quot;</span><span class="p">,</span> <span class="s2">&quot;sq&quot;</span><span class="p">]):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_computerijs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_computerjks</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_geomops2</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">)</span><span class="o">.</span><span class="n">isdisjoint</span><span class="p">([</span><span class="s2">&quot;q2&quot;</span><span class="p">,</span> <span class="s2">&quot;q4&quot;</span><span class="p">,</span> <span class="s2">&quot;q6&quot;</span><span class="p">]):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_computerijs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_boops</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="s2">&quot;q2&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_max_trig_order</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="s2">&quot;q4&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_max_trig_order</span> <span class="o">=</span> <span class="mi">4</span>
        <span class="k">if</span> <span class="s2">&quot;q6&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_max_trig_order</span> <span class="o">=</span> <span class="mi">6</span>

        <span class="c1"># Finish parameter treatment.</span>
        <span class="k">if</span> <span class="n">cutoff</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cutoff</span> <span class="o">=</span> <span class="o">-</span><span class="n">cutoff</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_voroneigh</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">elif</span> <span class="n">cutoff</span> <span class="o">&gt;</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_voroneigh</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cutoff radius is zero!&quot;</span><span class="p">)</span>

        <span class="c1"># Further variable definitions.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_last_nneigh</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">num_ops</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns:</span>
<span class="sd">            int: the number of different order parameters that are targeted</span>
<span class="sd">                to be calculated.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">last_nneigh</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns:</span>
<span class="sd">            int: the number of neighbors encountered during the most</span>
<span class="sd">                recent order parameter calculation. A value of -1 indicates</span>
<span class="sd">                that no such calculation has yet been performed for this</span>
<span class="sd">                instance.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_last_nneigh</span><span class="p">)</span>

<div class="viewcode-block" id="LocalStructOrderParams.compute_trigonometric_terms"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams.compute_trigonometric_terms">[docs]</a>    <span class="k">def</span> <span class="nf">compute_trigonometric_terms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">thetas</span><span class="p">,</span> <span class="n">phis</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes trigonometric terms that are required to</span>
<span class="sd">        calculate bond orientational order parameters using</span>
<span class="sd">        internal variables.</span>

<span class="sd">        Args:</span>
<span class="sd">            thetas ([float]): polar angles of all neighbors in radians.</span>
<span class="sd">            phis ([float]): azimuth angles of all neighbors in radians.</span>
<span class="sd">                The list of</span>
<span class="sd">                azimuth angles of all neighbors in radians.  The list of</span>
<span class="sd">                azimuth angles is expected to have the same size as the</span>
<span class="sd">                list of polar angles; otherwise, a ValueError is raised.</span>
<span class="sd">                Also, the two lists of angles have to be coherent in</span>
<span class="sd">                order. That is, it is expected that the order in the list</span>
<span class="sd">                of azimuth angles corresponds to a distinct sequence of</span>
<span class="sd">                neighbors. And, this sequence has to equal the sequence</span>
<span class="sd">                of neighbors in the list of polar angles.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">thetas</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">phis</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;List of polar and azimuthal angles have to be&quot;</span>
                             <span class="s2">&quot; equal!&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">sin</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">t</span><span class="p">))</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">thetas</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">cos</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">t</span><span class="p">))</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">thetas</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">sin</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">p</span><span class="p">))</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">phis</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">cos</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">p</span><span class="p">))</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">phis</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max_trig_order</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">e</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">e</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">sin</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="nb">float</span><span class="p">(</span><span class="n">p</span><span class="p">))</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">phis</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">cos</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="nb">float</span><span class="p">(</span><span class="n">p</span><span class="p">))</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">phis</span><span class="p">]</span></div>

<div class="viewcode-block" id="LocalStructOrderParams.get_q2"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams.get_q2">[docs]</a>    <span class="k">def</span> <span class="nf">get_q2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">thetas</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">phis</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the value of the bond orientational order parameter of</span>
<span class="sd">        weight l=2.  If the function is called with non-empty lists of</span>
<span class="sd">        polar and azimuthal angles the corresponding trigonometric terms</span>
<span class="sd">        are computed afresh.  Otherwise, it is expected that the</span>
<span class="sd">        compute_trigonometric_terms function has been just called.</span>

<span class="sd">        Args:</span>
<span class="sd">            thetas ([float]): polar angles of all neighbors in radians.</span>
<span class="sd">            phis ([float]): azimuth angles of all neighbors in radians.</span>

<span class="sd">        Returns:</span>
<span class="sd">            float: bond orientational order parameter of weight l=2</span>
<span class="sd">                corresponding to the input angles thetas and phis.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">thetas</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">phis</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">compute_trigonometric_terms</span><span class="p">(</span><span class="n">thetas</span><span class="p">,</span> <span class="n">phis</span><span class="p">)</span>
        <span class="n">nnn</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">nnn_range</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">nnn</span><span class="p">)</span>

        <span class="n">sqrt_15_2pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">15.0</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">pi</span><span class="p">))</span>
        <span class="n">sqrt_5_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">5.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>

        <span class="n">pre_y_2_2</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.25</span> <span class="o">*</span> <span class="n">sqrt_15_2pi</span> <span class="o">*</span> <span class="n">val</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
        <span class="n">pre_y_2_1</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">sqrt_15_2pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                     <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>

        <span class="n">acc</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="c1"># Y_2_-2</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_2_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_2_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_2_-1</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_2_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_2_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_2_0</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="mf">0.25</span> <span class="o">*</span> <span class="n">sqrt_5_pi</span> <span class="o">*</span> <span class="p">(</span><span class="mf">3.0</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span><span class="p">)</span>

        <span class="c1"># Y_2_1</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">-=</span> <span class="n">pre_y_2_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_2_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_2_2</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_2_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">+=</span> <span class="n">pre_y_2_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="n">q2</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">4.0</span> <span class="o">*</span> <span class="n">pi</span> <span class="o">*</span> <span class="n">acc</span> <span class="o">/</span> <span class="p">(</span><span class="mf">5.0</span> <span class="o">*</span> <span class="nb">float</span><span class="p">(</span><span class="n">nnn</span> <span class="o">*</span> <span class="n">nnn</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">q2</span></div>

<div class="viewcode-block" id="LocalStructOrderParams.get_q4"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams.get_q4">[docs]</a>    <span class="k">def</span> <span class="nf">get_q4</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">thetas</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">phis</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the value of the bond orientational order parameter of</span>
<span class="sd">        weight l=4.  If the function is called with non-empty lists of</span>
<span class="sd">        polar and azimuthal angles the corresponding trigonometric terms</span>
<span class="sd">        are computed afresh.  Otherwise, it is expected that the</span>
<span class="sd">        compute_trigonometric_terms function has been just called.</span>

<span class="sd">        Args:</span>
<span class="sd">            thetas ([float]): polar angles of all neighbors in radians.</span>
<span class="sd">            phis ([float]): azimuth angles of all neighbors in radians.</span>

<span class="sd">        Returns:</span>
<span class="sd">            float: bond orientational order parameter of weight l=4</span>
<span class="sd">                corresponding to the input angles thetas and phis.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">thetas</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">phis</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">compute_trigonometric_terms</span><span class="p">(</span><span class="n">thetas</span><span class="p">,</span> <span class="n">phis</span><span class="p">)</span>
        <span class="n">nnn</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">nnn_range</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">nnn</span><span class="p">)</span>

        <span class="n">i16_3</span> <span class="o">=</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="mf">16.0</span>
        <span class="n">i8_3</span> <span class="o">=</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="mf">8.0</span>

        <span class="n">sqrt_35_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">35.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>
        <span class="n">sqrt_35_2pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">35.0</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">pi</span><span class="p">))</span>
        <span class="n">sqrt_5_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">5.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>
        <span class="n">sqrt_5_2pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">5.0</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">pi</span><span class="p">))</span>
        <span class="n">sqrt_1_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>

        <span class="n">pre_y_4_4</span> <span class="o">=</span> <span class="p">[</span><span class="n">i16_3</span> <span class="o">*</span> <span class="n">sqrt_35_2pi</span> <span class="o">*</span> <span class="n">val</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">4</span><span class="p">]]</span>
        <span class="n">pre_y_4_3</span> <span class="o">=</span> <span class="p">[</span><span class="n">i8_3</span> <span class="o">*</span> <span class="n">sqrt_35_pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                     <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
        <span class="n">pre_y_4_2</span> <span class="o">=</span> <span class="p">[</span><span class="n">i8_3</span> <span class="o">*</span> <span class="n">sqrt_5_2pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mf">7.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span>
                     <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">2</span><span class="p">])]</span>
        <span class="n">pre_y_4_1</span> <span class="o">=</span> <span class="p">[</span><span class="n">i8_3</span> <span class="o">*</span> <span class="n">sqrt_5_pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mf">7.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="mf">3.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
                     <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span>
                                    <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>

        <span class="n">acc</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="c1"># Y_4_-4</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_4_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_4_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_4_-3</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_4_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_4_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_4_-2</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_4_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_4_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_4_-1</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_4_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_4_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_4_0</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">i16_3</span> <span class="o">*</span> <span class="n">sqrt_1_pi</span> <span class="o">*</span> <span class="p">(</span><span class="mf">35.0</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span>
                                         <span class="mf">30.0</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="mf">3.0</span><span class="p">)</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span><span class="p">)</span>

        <span class="c1"># Y_4_1</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">-=</span> <span class="n">pre_y_4_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_4_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_4_2</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_4_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">+=</span> <span class="n">pre_y_4_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_4_3</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">-=</span> <span class="n">pre_y_4_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_4_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_4_4</span>
        <span class="n">real</span> <span class="o">=</span> <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_4_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">+=</span> <span class="n">pre_y_4_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="n">q4</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">4.0</span> <span class="o">*</span> <span class="n">pi</span> <span class="o">*</span> <span class="n">acc</span> <span class="o">/</span> <span class="p">(</span><span class="mf">9.0</span> <span class="o">*</span> <span class="nb">float</span><span class="p">(</span><span class="n">nnn</span> <span class="o">*</span> <span class="n">nnn</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">q4</span></div>

<div class="viewcode-block" id="LocalStructOrderParams.get_q6"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams.get_q6">[docs]</a>    <span class="k">def</span> <span class="nf">get_q6</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">thetas</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">phis</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the value of the bond orientational order parameter of</span>
<span class="sd">        weight l=6.  If the function is called with non-empty lists of</span>
<span class="sd">        polar and azimuthal angles the corresponding trigonometric terms</span>
<span class="sd">        are computed afresh.  Otherwise, it is expected that the</span>
<span class="sd">        compute_trigonometric_terms function has been just called.</span>

<span class="sd">        Args:</span>
<span class="sd">            thetas ([float]): polar angles of all neighbors in radians.</span>
<span class="sd">            phis ([float]): azimuth angles of all neighbors in radians.</span>

<span class="sd">        Returns:</span>
<span class="sd">            float: bond orientational order parameter of weight l=6</span>
<span class="sd">                corresponding to the input angles thetas and phis.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">thetas</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">phis</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">compute_trigonometric_terms</span><span class="p">(</span><span class="n">thetas</span><span class="p">,</span> <span class="n">phis</span><span class="p">)</span>
        <span class="n">nnn</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">nnn_range</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">nnn</span><span class="p">)</span>

        <span class="n">i64</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mf">64.0</span>
        <span class="n">i32</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mf">32.0</span>
        <span class="n">i32_3</span> <span class="o">=</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="mf">32.0</span>
        <span class="n">i16</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mf">16.0</span>

        <span class="n">sqrt_3003_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">3003.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>
        <span class="n">sqrt_1001_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">1001.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>
        <span class="n">sqrt_91_2pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">91.0</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">pi</span><span class="p">))</span>
        <span class="n">sqrt_1365_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">1365.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>
        <span class="n">sqrt_273_2pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">273.0</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">pi</span><span class="p">))</span>
        <span class="n">sqrt_13_pi</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">13.0</span> <span class="o">/</span> <span class="n">pi</span><span class="p">)</span>

        <span class="n">pre_y_6_6</span> <span class="o">=</span> <span class="p">[</span><span class="n">i64</span> <span class="o">*</span> <span class="n">sqrt_3003_pi</span> <span class="o">*</span> <span class="n">val</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">6</span><span class="p">]]</span>
        <span class="n">pre_y_6_5</span> <span class="o">=</span> <span class="p">[</span><span class="n">i32_3</span> <span class="o">*</span> <span class="n">sqrt_1001_pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                     <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
        <span class="n">pre_y_6_4</span> <span class="o">=</span> <span class="p">[</span><span class="n">i32_3</span> <span class="o">*</span> <span class="n">sqrt_91_2pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mf">11.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span>
                     <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">2</span><span class="p">])]</span>
        <span class="n">pre_y_6_3</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">i32</span> <span class="o">*</span> <span class="n">sqrt_1365_pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mf">11.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="mf">3.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span>
                           <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
        <span class="n">pre_y_6_2</span> <span class="o">=</span> <span class="p">[</span><span class="n">i64</span> <span class="o">*</span> <span class="n">sqrt_1365_pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mf">33.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span>
                                                    <span class="mf">18.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span>
                     <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">2</span><span class="p">])]</span>
        <span class="n">pre_y_6_1</span> <span class="o">=</span> <span class="p">[</span><span class="n">i16</span> <span class="o">*</span> <span class="n">sqrt_273_2pi</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mf">33.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span>
                                                    <span class="mf">30.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mf">5.0</span> <span class="o">*</span> <span class="n">val</span><span class="p">[</span>
                                                        <span class="mi">3</span><span class="p">])</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span>
                     <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pow_sin_t</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
                         <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span>
                         <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>

        <span class="n">acc</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="c1"># Y_6_-6</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_6</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">6</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>  <span class="c1"># cos(x) =  cos(-x)</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_6</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">6</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>  <span class="c1"># sin(x) = -sin(-x)</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_-5</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_5</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">5</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_5</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">5</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_-4</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_-3</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_-2</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_-1</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_0</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">i32</span> <span class="o">*</span> <span class="n">sqrt_13_pi</span> <span class="o">*</span> <span class="p">(</span><span class="mf">231.0</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">6</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span>
                                        <span class="mf">315.0</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="mf">105.0</span> <span class="o">*</span>
                                        <span class="bp">self</span><span class="o">.</span><span class="n">_pow_cos_t</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mf">5.0</span><span class="p">)</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span><span class="p">)</span>

        <span class="c1"># Y_6_1</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">-=</span> <span class="n">pre_y_6_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_2</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">+=</span> <span class="n">pre_y_6_2</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_3</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">-=</span> <span class="n">pre_y_6_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_3</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">3</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_4</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">+=</span> <span class="n">pre_y_6_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_5</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">-=</span> <span class="n">pre_y_6_5</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">5</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">-=</span> <span class="n">pre_y_6_5</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">5</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="c1"># Y_6_6</span>
        <span class="n">real</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">imag</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nnn_range</span><span class="p">:</span>
            <span class="n">real</span> <span class="o">+=</span> <span class="n">pre_y_6_6</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cos_n_p</span><span class="p">[</span><span class="mi">6</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
            <span class="n">imag</span> <span class="o">+=</span> <span class="n">pre_y_6_6</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sin_n_p</span><span class="p">[</span><span class="mi">6</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="p">(</span><span class="n">real</span> <span class="o">*</span> <span class="n">real</span> <span class="o">+</span> <span class="n">imag</span> <span class="o">*</span> <span class="n">imag</span><span class="p">)</span>

        <span class="n">q6</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">4.0</span> <span class="o">*</span> <span class="n">pi</span> <span class="o">*</span> <span class="n">acc</span> <span class="o">/</span> <span class="p">(</span><span class="mf">13.0</span> <span class="o">*</span> <span class="nb">float</span><span class="p">(</span><span class="n">nnn</span> <span class="o">*</span> <span class="n">nnn</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">q6</span></div>

<div class="viewcode-block" id="LocalStructOrderParams.get_type"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams.get_type">[docs]</a>    <span class="k">def</span> <span class="nf">get_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return type of order parameter at the index provided and</span>
<span class="sd">        represented by a short string.</span>

<span class="sd">        Args:</span>
<span class="sd">            index (int): index of order parameter for which type is</span>
<span class="sd">                to be returned.</span>
<span class="sd">        Returns:</span>
<span class="sd">            str: OP type.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Index for getting order parameter type&quot;</span>
                             <span class="s2">&quot; out-of-bounds!&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">[</span><span class="n">index</span><span class="p">]</span></div>

<div class="viewcode-block" id="LocalStructOrderParams.get_parameters"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams.get_parameters">[docs]</a>    <span class="k">def</span> <span class="nf">get_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns list of floats that represents</span>
<span class="sd">        the parameters associated</span>
<span class="sd">        with calculation of the order</span>
<span class="sd">        parameter that was defined at the index provided.</span>
<span class="sd">        Attention: the parameters do not need to equal those originally</span>
<span class="sd">        inputted because of processing out of efficiency reasons.</span>

<span class="sd">        Args:</span>
<span class="sd">            index (int):</span>
<span class="sd">                index of order parameter for which associated parameters</span>
<span class="sd">                are to be returned.</span>
<span class="sd">        Returns:</span>
<span class="sd">            [float]: parameters of a given OP.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Index for getting parameters associated with&quot;</span>
                             <span class="s2">&quot; order parameter calculation out-of-bounds!&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">index</span><span class="p">]</span></div>

<div class="viewcode-block" id="LocalStructOrderParams.get_order_parameters"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.LocalStructOrderParams.get_order_parameters">[docs]</a>    <span class="k">def</span> <span class="nf">get_order_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">indices_neighs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">target_spec</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute all order parameters of site n.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (int): index of site in input structure,</span>
<span class="sd">                for which OPs are to be</span>
<span class="sd">                calculated.  Note that we do not use the sites iterator</span>
<span class="sd">                here, but directly access sites via struct[index].</span>
<span class="sd">            indices_neighs ([int]): list of indices of those neighbors</span>
<span class="sd">                in Structure object</span>
<span class="sd">                structure that are to be considered for OP computation.</span>
<span class="sd">                This optional argument overwrites the way neighbors are</span>
<span class="sd">                to be determined as defined in the constructor (i.e.,</span>
<span class="sd">                Voronoi coordination finder via negative cutoff radius</span>
<span class="sd">                vs constant cutoff radius if cutoff was positive).</span>
<span class="sd">                We do not use information about the underlying</span>
<span class="sd">                structure lattice if the neighbor indices are explicitly</span>
<span class="sd">                provided.  This has two important consequences.  First,</span>
<span class="sd">                the input Structure object can, in fact, be a</span>
<span class="sd">                simple list of Site objects.  Second, no nearest images</span>
<span class="sd">                of neighbors are determined when providing an index list.</span>
<span class="sd">                Note furthermore that this neighbor</span>
<span class="sd">                determination type ignores the optional target_spec</span>
<span class="sd">                argument.</span>
<span class="sd">            tol (float): threshold of weight</span>
<span class="sd">                (= solid angle / maximal solid angle)</span>
<span class="sd">                to determine if a particular pair is</span>
<span class="sd">                considered neighbors; this is relevant only in the case</span>
<span class="sd">                when Voronoi polyhedra are used to determine coordination</span>
<span class="sd">            target_spec (Specie): target species to be considered</span>
<span class="sd">                when calculating the order</span>
<span class="sd">                parameters of site n; None includes all species of input</span>
<span class="sd">                structure.</span>

<span class="sd">        Returns:</span>
<span class="sd">            [floats]: representing order parameters.  Should it not be</span>
<span class="sd">            possible to compute a given OP for a conceptual reason, the</span>
<span class="sd">            corresponding entry is None instead of a float.  For Steinhardt</span>
<span class="sd">            et al.&#39;s bond orientational OPs and the other geometric OPs</span>
<span class="sd">            (&quot;tet&quot;, &quot;oct&quot;, &quot;bcc&quot;, etc.),</span>
<span class="sd">            this can happen if there is a single</span>
<span class="sd">            neighbor around site n in the structure because that</span>
<span class="sd">            does not permit calculation of angles between multiple</span>
<span class="sd">            neighbors.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Do error-checking and initialization.</span>
        <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Site index smaller zero!&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">n</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Site index beyond maximum!&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">indices_neighs</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">indices_neighs</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Neighbor site index beyond maximum!&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">tol</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Negative tolerance for weighted solid angle!&quot;</span><span class="p">)</span>

        <span class="n">left_of_unity</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="mf">1.0e-12</span>
        <span class="c1"># The following threshold has to be adapted to non-Angstrom units.</span>
        <span class="n">very_small</span> <span class="o">=</span> <span class="mf">1.0e-12</span>
        <span class="n">fac_bcc</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span><span class="p">)</span>

        <span class="c1"># Find central site and its neighbors.</span>
        <span class="c1"># Note that we adopt the same way of accessing sites here as in</span>
        <span class="c1"># VoronoiNN; that is, not via the sites iterator.</span>
        <span class="n">centsite</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">indices_neighs</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">neighsites</span> <span class="o">=</span> <span class="p">[</span><span class="n">structure</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">indices_neighs</span><span class="p">]</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_voroneigh</span><span class="p">:</span>
            <span class="n">vnn</span> <span class="o">=</span> <span class="n">VoronoiNN</span><span class="p">(</span><span class="n">tol</span><span class="o">=</span><span class="n">tol</span><span class="p">,</span> <span class="n">targets</span><span class="o">=</span><span class="n">target_spec</span><span class="p">)</span>
            <span class="n">neighsites</span> <span class="o">=</span> <span class="n">vnn</span><span class="o">.</span><span class="n">get_nn</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Structure.get_sites_in_sphere --&gt; also other periodic images</span>
            <span class="n">neighsitestmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_sites_in_sphere</span><span class="p">(</span>
                <span class="n">centsite</span><span class="o">.</span><span class="n">coords</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cutoff</span><span class="p">)]</span>
            <span class="n">neighsites</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">if</span> <span class="n">centsite</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">neighsitestmp</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Could not find center site!&quot;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">neighsitestmp</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">centsite</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">target_spec</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">neighsites</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">neighsitestmp</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">neighsites</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[</span><span class="n">site</span> <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">neighsitestmp</span> <span class="k">if</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">symbol</span> <span class="o">==</span> <span class="n">target_spec</span><span class="p">]</span>
        <span class="n">nneigh</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">neighsites</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_last_nneigh</span> <span class="o">=</span> <span class="n">nneigh</span>

        <span class="c1"># Prepare angle calculations, if applicable.</span>
        <span class="n">rij</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">rjk</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">rijnorm</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">rjknorm</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">dist</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">distjk_unique</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">distjk</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">centvec</span> <span class="o">=</span> <span class="n">centsite</span><span class="o">.</span><span class="n">coords</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_computerijs</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">neigh</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">neighsites</span><span class="p">):</span>
                <span class="n">rij</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">neigh</span><span class="o">.</span><span class="n">coords</span> <span class="o">-</span> <span class="n">centvec</span><span class="p">))</span>
                <span class="n">dist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">rij</span><span class="p">[</span><span class="n">j</span><span class="p">]))</span>
                <span class="n">rijnorm</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">rij</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">/</span> <span class="n">dist</span><span class="p">[</span><span class="n">j</span><span class="p">]))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_computerjks</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">neigh</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">neighsites</span><span class="p">):</span>
                <span class="n">rjk</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
                <span class="n">rjknorm</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
                <span class="n">distjk</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
                <span class="n">kk</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">neighsites</span><span class="p">)):</span>
                    <span class="k">if</span> <span class="n">j</span> <span class="o">!=</span> <span class="n">k</span><span class="p">:</span>
                        <span class="n">rjk</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">neighsites</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">coords</span> <span class="o">-</span> <span class="n">neigh</span><span class="o">.</span><span class="n">coords</span><span class="p">)</span>
                        <span class="n">distjk</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">rjk</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">kk</span><span class="p">]))</span>
                        <span class="k">if</span> <span class="n">k</span> <span class="o">&gt;</span> <span class="n">j</span><span class="p">:</span>
                            <span class="n">distjk_unique</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">distjk</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">kk</span><span class="p">])</span>
                        <span class="n">rjknorm</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rjk</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">kk</span><span class="p">]</span> <span class="o">/</span> <span class="n">distjk</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">kk</span><span class="p">])</span>
                        <span class="n">kk</span> <span class="o">=</span> <span class="n">kk</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="c1"># Initialize OP list and, then, calculate OPs.</span>
        <span class="n">ops</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.0</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">]</span>
        <span class="c1"># norms = [[[] for j in range(nneigh)] for t in self._types]</span>

        <span class="c1"># First, coordination number and distance-based OPs.</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;cn&quot;</span><span class="p">:</span>
                <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">nneigh</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;norm&#39;</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;sgl_bd&quot;</span><span class="p">:</span>
                <span class="n">dist_sorted</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">dist_sorted</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
                <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">dist_sorted</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="n">dist_sorted</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">dist_sorted</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

        <span class="c1"># Then, bond orientational OPs based on spherical harmonics</span>
        <span class="c1"># according to Steinhardt et al., Phys. Rev. B, 28, 784-805, 1983.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_boops</span><span class="p">:</span>
            <span class="n">thetas</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">phis</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">vec</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rijnorm</span><span class="p">):</span>

                <span class="c1"># z is North pole --&gt; theta between vec and (0, 0, 1)^T.</span>
                <span class="c1"># Because vec is normalized, dot product is simply vec[2].</span>
                <span class="n">thetas</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">acos</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">vec</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="mf">1.0</span><span class="p">))))</span>
                <span class="n">tmpphi</span> <span class="o">=</span> <span class="mf">0.0</span>

                <span class="c1"># Compute phi only if it is not (almost) perfectly</span>
                <span class="c1"># aligned with z-axis.</span>
                <span class="k">if</span> <span class="o">-</span><span class="n">left_of_unity</span> <span class="o">&lt;</span> <span class="n">vec</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">left_of_unity</span><span class="p">:</span>
                    <span class="c1"># x is prime meridian --&gt; phi between projection of vec</span>
                    <span class="c1"># into x-y plane and (1, 0, 0)^T</span>
                    <span class="n">tmpphi</span> <span class="o">=</span> <span class="n">acos</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">vec</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="p">(</span><span class="n">sqrt</span><span class="p">(</span>
                        <span class="n">vec</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">vec</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">vec</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">vec</span><span class="p">[</span><span class="mi">1</span><span class="p">])),</span> <span class="mf">1.0</span><span class="p">)))</span>
                    <span class="k">if</span> <span class="n">vec</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">:</span>
                        <span class="n">tmpphi</span> <span class="o">=</span> <span class="o">-</span><span class="n">tmpphi</span>
                <span class="n">phis</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmpphi</span><span class="p">)</span>

            <span class="c1"># Note that None flags that we have too few neighbors</span>
            <span class="c1"># for calculating BOOPS.</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;q2&quot;</span><span class="p">:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_q2</span><span class="p">(</span><span class="n">thetas</span><span class="p">,</span> <span class="n">phis</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span>
                        <span class="n">thetas</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;q4&quot;</span><span class="p">:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_q4</span><span class="p">(</span><span class="n">thetas</span><span class="p">,</span> <span class="n">phis</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span>
                        <span class="n">thetas</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;q6&quot;</span><span class="p">:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_q6</span><span class="p">(</span><span class="n">thetas</span><span class="p">,</span> <span class="n">phis</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span>
                        <span class="n">thetas</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">None</span>

        <span class="c1"># Then, deal with the Peters-style OPs that are tailor-made</span>
        <span class="c1"># to recognize common structural motifs</span>
        <span class="c1"># (Peters, J. Chem. Phys., 131, 244103, 2009;</span>
        <span class="c1">#  Zimmermann et al., J. Am. Chem. Soc., under revision, 2015).</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_geomops</span><span class="p">:</span>
            <span class="n">gaussthetak</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.0</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">]</span>  <span class="c1"># not used by all OPs</span>
            <span class="n">qsptheta</span> <span class="o">=</span> <span class="p">[[[]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">)]</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">]</span>
            <span class="n">norms</span> <span class="o">=</span> <span class="p">[[[]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">)]</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">]</span>
            <span class="n">ipi</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="n">pi</span>
            <span class="n">piover2</span> <span class="o">=</span> <span class="n">pi</span> <span class="o">/</span> <span class="mf">2.0</span>
            <span class="n">onethird</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mf">3.0</span>
            <span class="n">twothird</span> <span class="o">=</span> <span class="mf">2.0</span> <span class="o">/</span> <span class="mf">3.0</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">):</span>  <span class="c1"># Neighbor j is put to the North pole.</span>
                <span class="n">zaxis</span> <span class="o">=</span> <span class="n">rijnorm</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
                <span class="n">kc</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">):</span>  <span class="c1"># From neighbor k, we construct</span>
                    <span class="k">if</span> <span class="n">j</span> <span class="o">!=</span> <span class="n">k</span><span class="p">:</span>  <span class="c1"># the prime meridian.</span>
                        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">)):</span>
                            <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
                            <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                        <span class="n">tmp</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span>
                            <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">inner</span><span class="p">(</span><span class="n">zaxis</span><span class="p">,</span> <span class="n">rijnorm</span><span class="p">[</span><span class="n">k</span><span class="p">]),</span> <span class="mf">1.0</span><span class="p">))</span>
                        <span class="n">thetak</span> <span class="o">=</span> <span class="n">acos</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
                        <span class="n">xaxis</span> <span class="o">=</span> <span class="n">gramschmidt</span><span class="p">(</span><span class="n">rijnorm</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">zaxis</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">xaxis</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">very_small</span><span class="p">:</span>
                            <span class="n">flag_xaxis</span> <span class="o">=</span> <span class="kc">True</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">xaxis</span> <span class="o">=</span> <span class="n">xaxis</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">xaxis</span><span class="p">)</span>
                            <span class="n">flag_xaxis</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_comp_azi</span><span class="p">:</span>
                            <span class="n">flag_yaxis</span> <span class="o">=</span> <span class="kc">True</span>
                            <span class="n">yaxis</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">zaxis</span><span class="p">,</span> <span class="n">xaxis</span><span class="p">)</span>
                            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">yaxis</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">very_small</span><span class="p">:</span>
                                <span class="n">yaxis</span> <span class="o">=</span> <span class="n">yaxis</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">yaxis</span><span class="p">)</span>
                                <span class="n">flag_yaxis</span> <span class="o">=</span> <span class="kc">False</span>

                        <span class="c1"># Contributions of j-i-k angles, where i represents the</span>
                        <span class="c1"># central atom and j and k two of the neighbors.</span>
                        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
                            <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;bent&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr_legacy&quot;</span><span class="p">]:</span>
                                <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                        <span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA&#39;</span><span class="p">])</span>
                                <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                                <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                            <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;tri_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;tet&quot;</span><span class="p">,</span>
                                       <span class="s2">&quot;tet_max&quot;</span><span class="p">]:</span>
                                <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                        <span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA&#39;</span><span class="p">])</span>
                                <span class="n">gaussthetak</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                                <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;tri_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;tet_max&quot;</span><span class="p">]:</span>
                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">gaussthetak</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                            <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;T&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_pyr&quot;</span><span class="p">,</span>
                                       <span class="s2">&quot;hex_pyr&quot;</span><span class="p">]:</span>
                                <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_EP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                        <span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span>
                                <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                                <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                            <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;sq_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;oct&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_legacy&quot;</span><span class="p">,</span>
                                       <span class="s2">&quot;cuboct&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct_max&quot;</span><span class="p">]:</span>
                                <span class="k">if</span> <span class="n">thetak</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                    <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_SPP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                            <span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span>
                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span>
                                            <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;w_SPP&#39;</span><span class="p">]</span> <span class="o">*</span>
                                            <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">))</span>
                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;w_SPP&#39;</span><span class="p">]</span>
                            <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;see_saw_rect&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_bipyr&quot;</span><span class="p">,</span>
                                       <span class="s2">&quot;pent_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_max&quot;</span><span class="p">,</span>
                                       <span class="s2">&quot;sq_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_plan_max&quot;</span><span class="p">]:</span>
                                <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                    <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_EP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                            <span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="k">if</span> <span class="n">t</span> <span class="o">!=</span> <span class="s2">&quot;hex_plan_max&quot;</span> <span class="k">else</span> \
                                        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                <span class="n">fabs</span><span class="p">(</span><span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">-</span>
                                                <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA&#39;</span><span class="p">])</span>
                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span>
                                        <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                            <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;pent_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_plan_max&quot;</span><span class="p">]:</span>
                                <span class="n">tmp</span> <span class="o">=</span> <span class="mf">0.4</span> <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                    <span class="s1">&#39;TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">pi</span> \
                                    <span class="k">else</span> <span class="mf">0.8</span>
                                <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                        <span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="n">tmp</span><span class="p">)</span>
                                <span class="n">gaussthetak</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                <span class="k">if</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;pent_plan_max&quot;</span><span class="p">:</span>
                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">gaussthetak</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                            <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;bcc&quot;</span> <span class="ow">and</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">:</span>
                                <span class="k">if</span> <span class="n">thetak</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                    <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_SPP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                            <span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span>
                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span>
                                            <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;w_SPP&#39;</span><span class="p">]</span> <span class="o">*</span>
                                            <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">))</span>
                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;w_SPP&#39;</span><span class="p">]</span>
                            <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;sq_face_cap_trig_pris&quot;</span><span class="p">:</span>
                                <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA3&#39;</span><span class="p">]:</span>
                                    <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA1&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">thetak</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA1&#39;</span><span class="p">])</span>
                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>

                        <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">):</span>
                            <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">!=</span> <span class="n">j</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">m</span> <span class="o">!=</span> <span class="n">k</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">flag_xaxis</span><span class="p">):</span>
                                <span class="n">tmp</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">inner</span><span class="p">(</span><span class="n">zaxis</span><span class="p">,</span> <span class="n">rijnorm</span><span class="p">[</span><span class="n">m</span><span class="p">]),</span> <span class="mf">1.0</span><span class="p">))</span>
                                <span class="n">thetam</span> <span class="o">=</span> <span class="n">acos</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
                                <span class="n">xtwoaxistmp</span> <span class="o">=</span> <span class="n">gramschmidt</span><span class="p">(</span><span class="n">rijnorm</span><span class="p">[</span><span class="n">m</span><span class="p">],</span> <span class="n">zaxis</span><span class="p">)</span>
                                <span class="n">l</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">xtwoaxistmp</span><span class="p">)</span>
                                <span class="k">if</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="n">very_small</span><span class="p">:</span>
                                    <span class="n">flag_xtwoaxis</span> <span class="o">=</span> <span class="kc">True</span>
                                <span class="k">else</span><span class="p">:</span>
                                    <span class="n">xtwoaxis</span> <span class="o">=</span> <span class="n">xtwoaxistmp</span> <span class="o">/</span> <span class="n">l</span>
                                    <span class="n">phi</span> <span class="o">=</span> <span class="n">acos</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">inner</span><span class="p">(</span><span class="n">xtwoaxis</span><span class="p">,</span> <span class="n">xaxis</span><span class="p">),</span> <span class="mf">1.0</span><span class="p">)))</span>
                                    <span class="n">flag_xtwoaxis</span> <span class="o">=</span> <span class="kc">False</span>
                                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_comp_azi</span><span class="p">:</span>
                                        <span class="n">phi2</span> <span class="o">=</span> <span class="n">atan2</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">xtwoaxis</span><span class="p">,</span> <span class="n">yaxis</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">xtwoaxis</span><span class="p">,</span> <span class="n">xaxis</span><span class="p">))</span>
                                <span class="c1"># South pole contributions of m.</span>
                                <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;tri_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_bipyr&quot;</span><span class="p">,</span>
                                         <span class="s2">&quot;hex_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_max&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_plan_max&quot;</span><span class="p">,</span>
                                         <span class="s2">&quot;hex_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;see_saw_rect&quot;</span><span class="p">]:</span>
                                    <span class="k">if</span> <span class="n">thetam</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                        <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_SPP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                <span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span>
                                        <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span>
                                            <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                                        <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>

                                <span class="c1"># Contributions of j-i-m angle and</span>
                                <span class="c1"># angles between plane j-i-k and i-m vector.</span>
                                <span class="k">if</span> <span class="ow">not</span> <span class="n">flag_xaxis</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">flag_xtwoaxis</span><span class="p">:</span>
                                    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
                                        <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;tri_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_plan_max&quot;</span><span class="p">,</span>
                                                 <span class="s2">&quot;tet&quot;</span><span class="p">,</span> <span class="s2">&quot;tet_max&quot;</span><span class="p">]:</span>
                                            <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA&#39;</span><span class="p">])</span>
                                            <span class="n">tmp2</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;fac_AA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">phi</span><span class="p">)</span> <span class="o">**</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;exp_cos_AA&#39;</span><span class="p">]</span>
                                            <span class="n">tmp3</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;tri_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;tet_max&quot;</span><span class="p">]</span> <span class="k">else</span> <span class="n">gaussthetak</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                                            <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp3</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span> <span class="o">*</span> <span class="n">tmp2</span>
                                            <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;pent_plan&quot;</span><span class="p">,</span>
                                                   <span class="s2">&quot;pent_plan_max&quot;</span><span class="p">]:</span>
                                            <span class="n">tmp</span> <span class="o">=</span> <span class="mf">0.4</span> <span class="k">if</span> <span class="n">thetam</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">pi</span> <span class="k">else</span> <span class="mf">0.8</span>
                                            <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="n">tmp</span><span class="p">)</span>
                                            <span class="n">tmp3</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">phi</span><span class="p">)</span>
                                            <span class="n">tmp4</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;pent_plan_max&quot;</span> <span class="k">else</span> <span class="n">gaussthetak</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                                            <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp4</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span> <span class="o">*</span> <span class="n">tmp3</span> <span class="o">*</span> <span class="n">tmp3</span>
                                            <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;T&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr&quot;</span><span class="p">,</span>
                                                   <span class="s2">&quot;pent_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_pyr&quot;</span><span class="p">]:</span>
                                            <span class="n">tmp</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;fac_AA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">phi</span><span class="p">)</span> <span class="o">**</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;exp_cos_AA&#39;</span><span class="p">]</span>
                                            <span class="n">tmp3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_EP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span>
                                            <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span>
                                                <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp3</span> <span class="o">*</span> <span class="n">tmp3</span><span class="p">)</span>
                                            <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;sq_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;oct&quot;</span><span class="p">,</span>
                                                   <span class="s2">&quot;oct_legacy&quot;</span><span class="p">]:</span>
                                            <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]</span> <span class="ow">and</span> \
                                                    <span class="n">thetam</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                                <span class="n">tmp</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;fac_AA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">phi</span><span class="p">)</span> <span class="o">**</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                    <span class="s1">&#39;exp_cos_AA&#39;</span><span class="p">]</span>
                                                <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_EP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span>
                                                <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                                <span class="k">if</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;oct_legacy&quot;</span><span class="p">:</span>
                                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">-=</span> <span class="n">tmp</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">6</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">7</span><span class="p">]</span>
                                                <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;tri_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_bipyr&quot;</span><span class="p">,</span>
                                                   <span class="s2">&quot;pent_bipyr&quot;</span><span class="p">,</span>
                                                   <span class="s2">&quot;hex_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_max&quot;</span><span class="p">,</span>
                                                   <span class="s2">&quot;sq_plan_max&quot;</span><span class="p">,</span>
                                                   <span class="s2">&quot;hex_plan_max&quot;</span><span class="p">]:</span>
                                            <span class="k">if</span> <span class="n">thetam</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                                <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                                    <span class="n">tmp</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                                  <span class="s1">&#39;fac_AA&#39;</span><span class="p">]</span> <span class="o">*</span>
                                                              <span class="n">phi</span><span class="p">)</span> <span class="o">**</span> \
                                                          <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                              <span class="s1">&#39;exp_cos_AA&#39;</span><span class="p">]</span>
                                                    <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                               <span class="s1">&#39;IGW_EP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                                   <span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="k">if</span> <span class="n">t</span> <span class="o">!=</span> <span class="s2">&quot;hex_plan_max&quot;</span> <span class="k">else</span> \
                                                        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                            <span class="s1">&#39;IGW_TA&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                                <span class="n">fabs</span><span class="p">(</span>
                                                                    <span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">-</span>
                                                                <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                                    <span class="s1">&#39;TA&#39;</span><span class="p">])</span>
                                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span>
                                                        <span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span>
                                                        <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;bcc&quot;</span> <span class="ow">and</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">:</span>
                                            <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                                <span class="k">if</span> <span class="n">thetak</span> <span class="o">&gt;</span> <span class="n">piover2</span><span class="p">:</span>
                                                    <span class="n">fac</span> <span class="o">=</span> <span class="mf">1.0</span>
                                                <span class="k">else</span><span class="p">:</span>
                                                    <span class="n">fac</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.0</span>
                                                <span class="n">tmp</span> <span class="o">=</span> <span class="p">(</span><span class="n">thetam</span> <span class="o">-</span> <span class="n">piover2</span><span class="p">)</span> <span class="o">/</span> <span class="n">asin</span><span class="p">(</span>
                                                    <span class="mi">1</span> <span class="o">/</span> <span class="mi">3</span><span class="p">)</span>
                                                <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">fac</span> <span class="o">*</span> <span class="n">cos</span><span class="p">(</span><span class="mf">3.0</span> <span class="o">*</span> <span class="n">phi</span><span class="p">)</span> <span class="o">*</span> <span class="n">fac_bcc</span> <span class="o">*</span> \
                                                    <span class="n">tmp</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                                                <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;see_saw_rect&quot;</span><span class="p">:</span>
                                            <span class="k">if</span> <span class="n">thetam</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]:</span>
                                                <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">phi</span> <span class="o">&lt;</span> <span class="mf">0.75</span> <span class="o">*</span> <span class="n">pi</span><span class="p">:</span>
                                                    <span class="n">tmp</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                                  <span class="s1">&#39;fac_AA&#39;</span><span class="p">]</span> <span class="o">*</span>
                                                              <span class="n">phi</span><span class="p">)</span> <span class="o">**</span> \
                                                          <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                              <span class="s1">&#39;exp_cos_AA&#39;</span><span class="p">]</span>
                                                    <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                               <span class="s1">&#39;IGW_EP&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                                   <span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span>
                                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">1.0</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;cuboct&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct_max&quot;</span><span class="p">]:</span>
                                            <span class="k">if</span> <span class="n">thetam</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;min_SPP&#39;</span><span class="p">]</span> <span class="ow">and</span> \
                                                    <span class="n">thetak</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">4</span><span class="p">]</span> <span class="ow">and</span> \
                                                    <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]:</span>
                                                <span class="k">if</span> <span class="n">thetam</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">4</span><span class="p">]</span> <span class="ow">and</span> <span class="n">thetam</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]:</span>
                                                    <span class="n">tmp</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">phi</span><span class="p">)</span>
                                                    <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                               <span class="mi">5</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                                   <span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span>
                                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span>
                                                        <span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span>
                                                        <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">1.0</span>
                                                <span class="k">elif</span> <span class="n">thetam</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">4</span><span class="p">]:</span>
                                                    <span class="n">tmp</span> <span class="o">=</span> <span class="mf">0.0556</span> <span class="o">*</span> <span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="n">phi</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">pi</span><span class="p">)</span> <span class="o">-</span> <span class="mf">0.81649658</span><span class="p">)</span>
                                                    <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">6</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="n">onethird</span><span class="p">)</span>
                                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span>
                                                        <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">1.0</span>
                                                <span class="k">elif</span> <span class="n">thetam</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]:</span>
                                                    <span class="n">tmp</span> <span class="o">=</span> <span class="mf">0.0556</span> <span class="o">*</span> <span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="n">phi</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">pi</span><span class="p">)</span> <span class="o">-</span> <span class="mf">0.81649658</span><span class="p">)</span>
                                                    <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">6</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                                   <span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span> <span class="n">twothird</span><span class="p">)</span>
                                                    <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span>
                                                        <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                                    <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">1.0</span>
                                        <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;sq_face_cap_trig_pris&quot;</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">flag_yaxis</span><span class="p">:</span>
                                            <span class="k">if</span> <span class="n">thetak</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA3&#39;</span><span class="p">]:</span>
                                                <span class="k">if</span> <span class="n">thetam</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA3&#39;</span><span class="p">]:</span>
                                                    <span class="n">tmp</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;fac_AA1&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">phi2</span><span class="p">)</span> <span class="o">**</span> \
                                                        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;exp_cos_AA1&#39;</span><span class="p">]</span>
                                                    <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;IGW_TA1&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span>
                                                                                         <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;TA1&#39;</span><span class="p">])</span>
                                                <span class="k">else</span><span class="p">:</span>
                                                    <span class="n">tmp</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;fac_AA2&#39;</span><span class="p">]</span> <span class="o">*</span>
                                                              <span class="p">(</span><span class="n">phi2</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;shift_AA2&#39;</span><span class="p">]))</span> <span class="o">**</span> \
                                                          <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;exp_cos_AA2&#39;</span><span class="p">]</span>
                                                    <span class="n">tmp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                               <span class="s1">&#39;IGW_TA2&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span>
                                                                   <span class="n">thetam</span> <span class="o">*</span> <span class="n">ipi</span> <span class="o">-</span>
                                                                   <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span>
                                                                       <span class="n">i</span><span class="p">][</span>
                                                                       <span class="s1">&#39;TA2&#39;</span><span class="p">])</span>

                                                <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span>
                                                    <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp2</span> <span class="o">*</span> <span class="n">tmp2</span><span class="p">)</span>
                                                <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">kc</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>

                        <span class="n">kc</span> <span class="o">+=</span> <span class="mi">1</span>

            <span class="c1"># Normalize Peters-style OPs.</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;tri_plan&quot;</span><span class="p">,</span> <span class="s2">&quot;tet&quot;</span><span class="p">,</span> <span class="s2">&quot;bent&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_plan&quot;</span><span class="p">,</span>
                         <span class="s2">&quot;oct&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_legacy&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_plan&quot;</span><span class="p">]:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp_norm</span> <span class="o">=</span> <span class="mf">0.0</span>
                    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">):</span>
                        <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])</span>
                        <span class="n">tmp_norm</span> <span class="o">+=</span> <span class="nb">float</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]))</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="n">tmp_norm</span> <span class="k">if</span> <span class="n">tmp_norm</span> <span class="o">&gt;</span> <span class="mf">1.0e-12</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="k">elif</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;T&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;see_saw_rect&quot;</span><span class="p">,</span> <span class="s2">&quot;sq_pyr&quot;</span><span class="p">,</span>
                           <span class="s2">&quot;tri_bipyr&quot;</span><span class="p">,</span>
                           <span class="s2">&quot;sq_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;hex_pyr&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_bipyr&quot;</span><span class="p">,</span>
                           <span class="s2">&quot;hex_bipyr&quot;</span><span class="p">,</span> <span class="s2">&quot;oct_max&quot;</span><span class="p">,</span> <span class="s2">&quot;tri_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;tet_max&quot;</span><span class="p">,</span>
                           <span class="s2">&quot;sq_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;pent_plan_max&quot;</span><span class="p">,</span> <span class="s2">&quot;cuboct_max&quot;</span><span class="p">,</span>
                           <span class="s2">&quot;hex_plan_max&quot;</span><span class="p">,</span>
                           <span class="s2">&quot;sq_face_cap_trig_pris&quot;</span><span class="p">]:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
                    <span class="k">if</span> <span class="n">nneigh</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">):</span>
                            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])):</span>
                                <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">/</span> <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> \
                                    <span class="k">if</span> <span class="n">norms</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mf">1.0e-12</span> <span class="k">else</span> <span class="mf">0.0</span>
                            <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="k">if</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span> \
                                <span class="k">else</span> <span class="nb">max</span><span class="p">(</span><span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="nb">max</span><span class="p">(</span><span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]))</span>
                <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;bcc&quot;</span><span class="p">:</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span>
                    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">):</span>
                        <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])</span>
                    <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="mf">0.5</span> <span class="o">*</span> <span class="nb">float</span><span class="p">(</span>
                        <span class="n">nneigh</span> <span class="o">*</span> <span class="p">(</span><span class="mi">6</span> <span class="o">+</span> <span class="p">(</span><span class="n">nneigh</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">nneigh</span> <span class="o">-</span> <span class="mi">3</span><span class="p">))))</span> \
                        <span class="k">if</span> <span class="n">nneigh</span> <span class="o">&gt;</span> <span class="mi">3</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;sq_pyr_legacy&quot;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">nneigh</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                        <span class="n">dmean</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
                        <span class="n">acc</span> <span class="o">=</span> <span class="mf">0.0</span>
                        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dist</span><span class="p">:</span>
                            <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">d</span> <span class="o">-</span> <span class="n">dmean</span><span class="p">)</span>
                            <span class="n">acc</span> <span class="o">=</span> <span class="n">acc</span> <span class="o">+</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">tmp</span> <span class="o">*</span> <span class="n">tmp</span><span class="p">)</span>
                        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nneigh</span><span class="p">):</span>
                            <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">])</span> <span class="k">if</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span> \
                                <span class="k">else</span> <span class="nb">max</span><span class="p">(</span><span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="nb">max</span><span class="p">(</span><span class="n">qsptheta</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]))</span>
                        <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">acc</span> <span class="o">*</span> <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">nneigh</span><span class="p">)</span>
                        <span class="c1"># nneigh * (nneigh - 1))</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Then, deal with the new-style OPs that require vectors between</span>
        <span class="c1"># neighbors.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_geomops2</span><span class="p">:</span>
            <span class="c1"># Compute all (unique) angles and sort the resulting list.</span>
            <span class="n">aij</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">ir</span><span class="p">,</span> <span class="n">r</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rijnorm</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ir</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">rijnorm</span><span class="p">)):</span>
                    <span class="n">aij</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">acos</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">inner</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">rijnorm</span><span class="p">[</span><span class="n">j</span><span class="p">]),</span> <span class="mf">1.0</span><span class="p">))))</span>
            <span class="n">aijs</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">aij</span><span class="p">)</span>

            <span class="c1"># Compute height, side and diagonal length estimates.</span>
            <span class="n">neighscent</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">neigh</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">neighsites</span><span class="p">):</span>
                <span class="n">neighscent</span> <span class="o">=</span> <span class="n">neighscent</span> <span class="o">+</span> <span class="n">neigh</span><span class="o">.</span><span class="n">coords</span>
            <span class="k">if</span> <span class="n">nneigh</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">neighscent</span> <span class="o">=</span> <span class="p">(</span><span class="n">neighscent</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">nneigh</span><span class="p">))</span>
            <span class="n">h</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">neighscent</span> <span class="o">-</span> <span class="n">centvec</span><span class="p">)</span>
            <span class="n">b</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">distjk_unique</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">distjk_unique</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">0</span>
            <span class="n">dhalf</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">distjk_unique</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.0</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">distjk_unique</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">0</span>

            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_types</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;reg_tri&quot;</span> <span class="ow">or</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;sq&quot;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">nneigh</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">:</span>
                        <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
                        <span class="k">if</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;reg_tri&quot;</span><span class="p">:</span>
                            <span class="n">a</span> <span class="o">=</span> <span class="mf">2.0</span> <span class="o">*</span> <span class="n">asin</span><span class="p">(</span><span class="n">b</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">h</span> <span class="o">*</span> <span class="n">h</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">/</span> <span class="p">(</span>
                                    <span class="mf">2.0</span> <span class="o">*</span> <span class="n">cos</span><span class="p">(</span><span class="mf">3.0</span> <span class="o">*</span> <span class="n">pi</span> <span class="o">/</span> <span class="mf">18.0</span><span class="p">)))</span> <span class="o">**</span> <span class="mf">2.0</span><span class="p">)))</span>
                            <span class="n">nmax</span> <span class="o">=</span> <span class="mi">3</span>
                        <span class="k">elif</span> <span class="n">t</span> <span class="o">==</span> <span class="s2">&quot;sq&quot;</span><span class="p">:</span>
                            <span class="n">a</span> <span class="o">=</span> <span class="mf">2.0</span> <span class="o">*</span> <span class="n">asin</span><span class="p">(</span>
                                <span class="n">b</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">h</span> <span class="o">*</span> <span class="n">h</span> <span class="o">+</span> <span class="n">dhalf</span> <span class="o">*</span> <span class="n">dhalf</span><span class="p">)))</span>
                            <span class="n">nmax</span> <span class="o">=</span> <span class="mi">4</span>
                        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">min</span><span class="p">([</span><span class="n">nneigh</span><span class="p">,</span> <span class="n">nmax</span><span class="p">])):</span>
                            <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">ops</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">((</span>
                                                                  <span class="n">aijs</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">-</span> <span class="n">a</span><span class="p">)</span> <span class="o">*</span>
                                                          <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="n">i</span><span class="p">][</span>
                                                              <span class="mi">0</span><span class="p">])</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">ops</span></div></div>


<div class="viewcode-block" id="BrunnerNN_reciprocal"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.BrunnerNN_reciprocal">[docs]</a><span class="k">class</span> <span class="nc">BrunnerNN_reciprocal</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine coordination number using Brunner&#39;s algorithm which counts the</span>
<span class="sd">    atoms that are within the largest gap in differences in real space</span>
<span class="sd">    interatomic distances. This algorithm uses Brunner&#39;s method of</span>
<span class="sd">    largest reciprocal gap in interatomic distances.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1.0e-4</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">8.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for bond determination</span>
<span class="sd">                (default: 1E-4).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for near-neighbor</span>
<span class="sd">                atoms. Defaults to 8.0.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="BrunnerNN_reciprocal.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.BrunnerNN_reciprocal.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                sites.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a coordinated site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="n">neighs_dists</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">)</span>
        <span class="n">ds</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">nn_distance</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">]</span>
        <span class="n">ds</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

        <span class="n">ns</span> <span class="o">=</span> <span class="p">[</span><span class="mf">1.0</span> <span class="o">/</span> <span class="n">ds</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="n">ds</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ds</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>

        <span class="n">d_max</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="n">ns</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">ns</span><span class="p">))]</span>
        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
            <span class="n">s</span><span class="p">,</span> <span class="n">dist</span> <span class="o">=</span> <span class="n">nn</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
            <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;</span> <span class="n">d_max</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">:</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">dist</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">s</span><span class="p">,</span>
                            <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">s</span><span class="p">),</span>
                            <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                            <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                                  <span class="n">s</span><span class="p">)})</span>
        <span class="k">return</span> <span class="n">siw</span></div></div>


<div class="viewcode-block" id="BrunnerNN_relative"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.BrunnerNN_relative">[docs]</a><span class="k">class</span> <span class="nc">BrunnerNN_relative</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine coordination number using Brunner&#39;s algorithm which counts the</span>
<span class="sd">    atoms that are within the largest gap in differences in real space</span>
<span class="sd">    interatomic distances. This algorithm uses Brunner&#39;s method of</span>
<span class="sd">    of largest relative gap in interatomic distances.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1.0e-4</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">8.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for bond determination</span>
<span class="sd">                (default: 1E-4).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for near-neighbor</span>
<span class="sd">                atoms. Defaults to 8.0.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="BrunnerNN_relative.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.BrunnerNN_relative.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                sites.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a coordinated site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="n">neighs_dists</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">)</span>
        <span class="n">ds</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">nn_distance</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">]</span>
        <span class="n">ds</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

        <span class="n">ns</span> <span class="o">=</span> <span class="p">[</span><span class="n">ds</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">ds</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ds</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>

        <span class="n">d_max</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="n">ns</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">ns</span><span class="p">))]</span>
        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
            <span class="n">s</span><span class="p">,</span> <span class="n">dist</span> <span class="o">=</span> <span class="n">nn</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
            <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;</span> <span class="n">d_max</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">:</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">dist</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">s</span><span class="p">,</span>
                            <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">s</span><span class="p">),</span>
                            <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                            <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                                  <span class="n">s</span><span class="p">)})</span>
        <span class="k">return</span> <span class="n">siw</span></div></div>


<div class="viewcode-block" id="BrunnerNN_real"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.BrunnerNN_real">[docs]</a><span class="k">class</span> <span class="nc">BrunnerNN_real</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine coordination number using Brunner&#39;s algorithm which counts the</span>
<span class="sd">    atoms that are within the largest gap in differences in real space</span>
<span class="sd">    interatomic distances. This algorithm uses Brunner&#39;s method of</span>
<span class="sd">    largest gap in interatomic distances.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1.0e-4</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">8.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol (float): tolerance parameter for bond determination</span>
<span class="sd">                (default: 1E-4).</span>
<span class="sd">            cutoff (float): cutoff radius in Angstrom to look for near-neighbor</span>
<span class="sd">                atoms. Defaults to 8.0.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="BrunnerNN_real.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.BrunnerNN_real.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                sites.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a coordinated site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="n">neighs_dists</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">)</span>
        <span class="n">ds</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">nn_distance</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">]</span>
        <span class="n">ds</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

        <span class="n">ns</span> <span class="o">=</span> <span class="p">[</span><span class="n">ds</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">ds</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ds</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>

        <span class="n">d_max</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="n">ns</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">ns</span><span class="p">))]</span>
        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
            <span class="n">s</span><span class="p">,</span> <span class="n">dist</span> <span class="o">=</span> <span class="n">nn</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
            <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;</span> <span class="n">d_max</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">:</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">dist</span>
                <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">s</span><span class="p">,</span>
                            <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">s</span><span class="p">),</span>
                            <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                            <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                                                  <span class="n">s</span><span class="p">)})</span>
        <span class="k">return</span> <span class="n">siw</span></div></div>


<div class="viewcode-block" id="EconNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.EconNN">[docs]</a><span class="k">class</span> <span class="nc">EconNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determines the average effective coordination number for each cation in a</span>
<span class="sd">    given structure using Hoppe&#39;s algorithm.</span>

<span class="sd">    This method follows the procedure outlined in:</span>

<span class="sd">    Hoppe, Rudolf. &quot;Effective coordination numbers (ECoN) and mean fictive ionic</span>
<span class="sd">    radii (MEFIR).&quot; Zeitschrift für Kristallographie-Crystalline Materials</span>
<span class="sd">    150.1-4 (1979): 23-52.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">tol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.2</span><span class="p">,</span>
        <span class="n">cutoff</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">10.0</span><span class="p">,</span>
        <span class="n">cation_anion</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
        <span class="n">use_fictive_radius</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            tol: Tolerance parameter for bond determination.</span>
<span class="sd">            cutoff: Cutoff radius in Angstrom to look for near-neighbor atoms.</span>
<span class="sd">            cation_anion: If set to True, will restrict bonding targets to</span>
<span class="sd">                sites with opposite or zero charge. Requires an oxidation states</span>
<span class="sd">                on all sites in the structure.</span>
<span class="sd">            use_fictive_radius: Whether to use the fictive radius in the</span>
<span class="sd">                EcoN calculation. If False, the bond distance will be used.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tol</span> <span class="o">=</span> <span class="n">tol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span> <span class="o">=</span> <span class="n">cutoff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cation_anion</span> <span class="o">=</span> <span class="n">cation_anion</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_fictive_radius</span> <span class="o">=</span> <span class="n">use_fictive_radius</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

<div class="viewcode-block" id="EconNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.EconNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                sites.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a coordinated site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="n">neighbors</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cation_anion</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">,</span> <span class="s2">&quot;oxi_state&quot;</span><span class="p">):</span>
            <span class="c1"># filter out neighbor of like charge (except for neutral sites)</span>
            <span class="k">if</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">oxi_state</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">neighbors</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">neighbors</span> <span class="k">if</span> <span class="n">n</span><span class="o">.</span><span class="n">oxi_state</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">oxi_state</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">neighbors</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">neighbors</span> <span class="k">if</span> <span class="n">n</span><span class="o">.</span><span class="n">oxi_state</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_fictive_radius</span><span class="p">:</span>
            <span class="c1"># calculate fictive ionic radii</span>
            <span class="n">firs</span> <span class="o">=</span> <span class="p">[</span><span class="n">_get_fictive_ionic_radius</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">neighbor</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">neighbor</span> <span class="ow">in</span> <span class="n">neighbors</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># just use the bond distance</span>
            <span class="n">firs</span> <span class="o">=</span> <span class="p">[</span><span class="n">neighbor</span><span class="o">.</span><span class="n">nn_distance</span> <span class="k">for</span> <span class="n">neighbor</span> <span class="ow">in</span> <span class="n">neighbors</span><span class="p">]</span>

        <span class="c1"># calculate mean fictive ionic radius</span>
        <span class="n">mefir</span> <span class="o">=</span> <span class="n">_get_mean_fictive_ionic_radius</span><span class="p">(</span><span class="n">firs</span><span class="p">)</span>

        <span class="c1"># # iteratively solve MEFIR; follows equation 4 in Hoppe&#39;s EconN paper</span>
        <span class="n">prev_mefir</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="s2">&quot;inf&quot;</span><span class="p">)</span>
        <span class="k">while</span> <span class="nb">abs</span><span class="p">(</span><span class="n">prev_mefir</span> <span class="o">-</span> <span class="n">mefir</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">1e-4</span><span class="p">:</span>
            <span class="c1"># this is guaranteed to converge</span>
            <span class="n">prev_mefir</span> <span class="o">=</span> <span class="n">mefir</span>
            <span class="n">mefir</span> <span class="o">=</span> <span class="n">_get_mean_fictive_ionic_radius</span><span class="p">(</span><span class="n">firs</span><span class="p">,</span> <span class="n">minimum_fir</span><span class="o">=</span><span class="n">mefir</span><span class="p">)</span>

        <span class="n">siw</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span><span class="p">,</span> <span class="n">fir</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">neighbors</span><span class="p">,</span> <span class="n">firs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">cutoff</span><span class="p">:</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="p">(</span><span class="n">fir</span> <span class="o">/</span> <span class="n">mefir</span><span class="p">)</span> <span class="o">**</span> <span class="mi">6</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">w</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">tol</span><span class="p">:</span>
                    <span class="n">bonded_site</span> <span class="o">=</span> <span class="p">{</span>
                        <span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">nn</span><span class="p">,</span>
                        <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">nn</span><span class="p">),</span>
                        <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">w</span><span class="p">,</span>
                        <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">nn</span><span class="p">)</span>
                    <span class="p">}</span>
                    <span class="n">siw</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bonded_site</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">siw</span></div></div>


<span class="k">def</span> <span class="nf">_get_fictive_ionic_radius</span><span class="p">(</span><span class="n">site</span><span class="p">:</span> <span class="n">Site</span><span class="p">,</span> <span class="n">neighbor</span><span class="p">:</span> <span class="n">PeriodicNeighbor</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Get fictive ionic radius.</span>

<span class="sd">    Follows equation 1 of:</span>

<span class="sd">    Hoppe, Rudolf. &quot;Effective coordination numbers (ECoN) and mean fictive ionic</span>
<span class="sd">    radii (MEFIR).&quot; Zeitschrift für Kristallographie-Crystalline Materials</span>
<span class="sd">    150.1-4 (1979): 23-52.</span>

<span class="sd">    Args:</span>
<span class="sd">        site: The central site.</span>
<span class="sd">        neighbor neighboring site.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Hoppe&#39;s fictive ionic radius.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">r_h</span> <span class="o">=</span> <span class="n">_get_radius</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">r_h</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">r_h</span> <span class="o">=</span> <span class="n">_get_default_radius</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>

    <span class="n">r_i</span> <span class="o">=</span> <span class="n">_get_radius</span><span class="p">(</span><span class="n">neighbor</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">r_i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">r_i</span> <span class="o">=</span> <span class="n">_get_default_radius</span><span class="p">(</span><span class="n">neighbor</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">neighbor</span><span class="o">.</span><span class="n">nn_distance</span> <span class="o">*</span> <span class="p">(</span><span class="n">r_h</span> <span class="o">/</span> <span class="p">(</span><span class="n">r_h</span> <span class="o">+</span> <span class="n">r_i</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">_get_mean_fictive_ionic_radius</span><span class="p">(</span>
        <span class="n">fictive_ionic_radii</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">],</span>
        <span class="n">minimum_fir</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the mean fictive ionic radius.</span>

<span class="sd">    Follows equation 2:</span>

<span class="sd">    Hoppe, Rudolf. &quot;Effective coordination numbers (ECoN) and mean fictive ionic</span>
<span class="sd">    radii (MEFIR).&quot; Zeitschrift für Kristallographie-Crystalline Materials</span>
<span class="sd">    150.1-4 (1979): 23-52.</span>

<span class="sd">    Args:</span>
<span class="sd">        fictive_ionic_radii: List of fictive ionic radii for a center site</span>
<span class="sd">            and its neighbors.</span>
<span class="sd">        minimum_fir: Minimum fictive ionic radius to use.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Hoppe&#39;s mean fictive ionic radius.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">minimum_fir</span><span class="p">:</span>
        <span class="n">minimum_fir</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">fictive_ionic_radii</span><span class="p">)</span>

    <span class="n">weighted_sum</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="n">total_sum</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="k">for</span> <span class="n">fir</span> <span class="ow">in</span> <span class="n">fictive_ionic_radii</span><span class="p">:</span>
        <span class="n">weighted_sum</span> <span class="o">+=</span> <span class="n">fir</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="p">(</span><span class="n">fir</span> <span class="o">/</span> <span class="n">minimum_fir</span><span class="p">)</span> <span class="o">**</span> <span class="mi">6</span><span class="p">)</span>
        <span class="n">total_sum</span> <span class="o">+=</span> <span class="n">exp</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="p">(</span><span class="n">fir</span> <span class="o">/</span> <span class="n">minimum_fir</span><span class="p">)</span> <span class="o">**</span> <span class="mi">6</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">weighted_sum</span> <span class="o">/</span> <span class="n">total_sum</span>


<div class="viewcode-block" id="CrystalNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CrystalNN">[docs]</a><span class="k">class</span> <span class="nc">CrystalNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This is custom near neighbor method intended for use in all kinds of</span>
<span class="sd">    periodic structures (metals, minerals, porous structures, etc). It is based</span>
<span class="sd">    on a Voronoi algorithm and uses the solid angle weights to determine the</span>
<span class="sd">    probability of various coordination environments. The algorithm can also</span>
<span class="sd">    modify probability using smooth distance cutoffs as well as Pauling</span>
<span class="sd">    electronegativity differences. The output can either be the most probable</span>
<span class="sd">    coordination environment or a weighted list of coordination environments.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">NNData</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s2">&quot;nn_data&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;all_nninfo&quot;</span><span class="p">,</span> <span class="s2">&quot;cn_weights&quot;</span><span class="p">,</span> <span class="s2">&quot;cn_nninfo&quot;</span><span class="p">])</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">weighted_cn</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">cation_anion</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">distance_cutoffs</span><span class="o">=</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">x_diff_weight</span><span class="o">=</span><span class="mf">3.0</span><span class="p">,</span>
                 <span class="n">porous_adjustment</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">search_cutoff</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span>
                 <span class="n">fingerprint_length</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialize CrystalNN with desired parameters. Default parameters assume</span>
<span class="sd">        &quot;chemical bond&quot; type behavior is desired. For geometric neighbor</span>
<span class="sd">        finding (e.g., structural framework), set (i) distance_cutoffs=None,</span>
<span class="sd">        (ii) x_diff_weight=0.0 and (optionally) (iii) porous_adjustment=False</span>
<span class="sd">        which will disregard the atomic identities and perform best for a purely</span>
<span class="sd">        geometric match.</span>

<span class="sd">        Args:</span>
<span class="sd">            weighted_cn: (bool) if set to True, will return fractional weights</span>
<span class="sd">                for each potential near neighbor.</span>
<span class="sd">            cation_anion: (bool) if set True, will restrict bonding targets to</span>
<span class="sd">                sites with opposite or zero charge. Requires an oxidation states</span>
<span class="sd">                on all sites in the structure.</span>
<span class="sd">            distance_cutoffs: ([float, float]) - if not None, penalizes neighbor</span>
<span class="sd">                distances greater than sum of covalent radii plus</span>
<span class="sd">                distance_cutoffs[0]. Distances greater than covalent radii sum</span>
<span class="sd">                plus distance_cutoffs[1] are enforced to have zero weight.</span>
<span class="sd">            x_diff_weight: (float) - if multiple types of neighbor elements are</span>
<span class="sd">                possible, this sets preferences for targets with higher</span>
<span class="sd">                electronegativity difference.</span>
<span class="sd">            porous_adjustment: (bool) - if True, readjusts Voronoi weights to</span>
<span class="sd">                better describe layered / porous structures</span>
<span class="sd">            search_cutoff: (float) cutoff in Angstroms for initial neighbor</span>
<span class="sd">                search; this will be adjusted if needed internally</span>
<span class="sd">            fingerprint_length: (int) if a fixed_length CN &quot;fingerprint&quot; is</span>
<span class="sd">                desired from get_nn_data(), set this parameter</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weighted_cn</span> <span class="o">=</span> <span class="n">weighted_cn</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cation_anion</span> <span class="o">=</span> <span class="n">cation_anion</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">distance_cutoffs</span> <span class="o">=</span> <span class="n">distance_cutoffs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x_diff_weight</span> <span class="o">=</span> <span class="n">x_diff_weight</span> <span class="k">if</span> <span class="n">x_diff_weight</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">search_cutoff</span> <span class="o">=</span> <span class="n">search_cutoff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">porous_adjustment</span> <span class="o">=</span> <span class="n">porous_adjustment</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fingerprint_length</span> <span class="o">=</span> <span class="n">fingerprint_length</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

<div class="viewcode-block" id="CrystalNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CrystalNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor information.</span>
<span class="sd">        Args:</span>
<span class="sd">            structure: (Structure) pymatgen Structure</span>
<span class="sd">            n: (int) index of target site</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of dicts): each dictionary provides information</span>
<span class="sd">                about a single near neighbor, where key &#39;site&#39; gives</span>
<span class="sd">                access to the corresponding Site object, &#39;image&#39; gives</span>
<span class="sd">                the image location, and &#39;weight&#39; provides the weight</span>
<span class="sd">                that a given near-neighbor site contributes</span>
<span class="sd">                to the coordination number (1 or smaller), &#39;site_index&#39;</span>
<span class="sd">                gives index of the corresponding site in</span>
<span class="sd">                the original structure.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">nndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nn_data</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">weighted_cn</span><span class="p">:</span>
            <span class="n">max_key</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">nndata</span><span class="o">.</span><span class="n">cn_weights</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">k</span><span class="p">:</span> <span class="n">nndata</span><span class="o">.</span><span class="n">cn_weights</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
            <span class="n">nn</span> <span class="o">=</span> <span class="n">nndata</span><span class="o">.</span><span class="n">cn_nninfo</span><span class="p">[</span><span class="n">max_key</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
                <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">return</span> <span class="n">nn</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nndata</span><span class="o">.</span><span class="n">all_nninfo</span><span class="p">:</span>
                <span class="n">weight</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">for</span> <span class="n">cn</span> <span class="ow">in</span> <span class="n">nndata</span><span class="o">.</span><span class="n">cn_nninfo</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">cn_entry</span> <span class="ow">in</span> <span class="n">nndata</span><span class="o">.</span><span class="n">cn_nninfo</span><span class="p">[</span><span class="n">cn</span><span class="p">]:</span>
                        <span class="k">if</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;site&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">cn_entry</span><span class="p">[</span><span class="s2">&quot;site&quot;</span><span class="p">]:</span>
                            <span class="n">weight</span> <span class="o">+=</span> <span class="n">nndata</span><span class="o">.</span><span class="n">cn_weights</span><span class="p">[</span><span class="n">cn</span><span class="p">]</span>

                <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">weight</span>

            <span class="k">return</span> <span class="n">nndata</span><span class="o">.</span><span class="n">all_nninfo</span></div>

<div class="viewcode-block" id="CrystalNN.get_nn_data"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CrystalNN.get_nn_data">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">length</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The main logic of the method to compute near neighbor.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: (Structure) enclosing structure object</span>
<span class="sd">            n: (int) index of target site to get NN info for</span>
<span class="sd">            length: (int) if set, will return a fixed range of CN numbers</span>

<span class="sd">        Returns:</span>
<span class="sd">            a namedtuple (NNData) object that contains:</span>
<span class="sd">                - all near neighbor sites with weights</span>
<span class="sd">                - a dict of CN -&gt; weight</span>
<span class="sd">                - a dict of CN -&gt; associated near neighbor sites</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">length</span> <span class="o">=</span> <span class="n">length</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">fingerprint_length</span>

        <span class="c1"># determine possible bond targets</span>
        <span class="n">target</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cation_anion</span><span class="p">:</span>
            <span class="n">target</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">m_oxi</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">oxi_state</span>
            <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">structure</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">oxi_state</span> <span class="o">*</span> <span class="n">m_oxi</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>  <span class="c1"># opposite charge</span>
                    <span class="n">target</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">target</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;No valid targets for site within cation_anion constraint!&quot;</span><span class="p">)</span>

        <span class="c1"># get base VoronoiNN targets</span>
        <span class="n">cutoff</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">search_cutoff</span>
        <span class="n">vnn</span> <span class="o">=</span> <span class="n">VoronoiNN</span><span class="p">(</span><span class="n">weight</span><span class="o">=</span><span class="s2">&quot;solid_angle&quot;</span><span class="p">,</span> <span class="n">targets</span><span class="o">=</span><span class="n">target</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="n">cutoff</span><span class="p">)</span>
        <span class="n">nn</span> <span class="o">=</span> <span class="n">vnn</span><span class="o">.</span><span class="n">get_nn_info</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>

        <span class="c1"># solid angle weights can be misleading in open / porous structures</span>
        <span class="c1"># adjust weights to correct for this behavior</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">porous_adjustment</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
                <span class="n">x</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">*=</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;poly_info&quot;</span><span class="p">][</span>
                                   <span class="s2">&quot;solid_angle&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;poly_info&quot;</span><span class="p">][</span><span class="s2">&quot;area&quot;</span><span class="p">]</span>

        <span class="c1"># adjust solid angle weight based on electronegativity difference</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">x_diff_weight</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
                <span class="n">X1</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">X</span>
                <span class="n">X2</span> <span class="o">=</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;site&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">X</span>

                <span class="k">if</span> <span class="n">math</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">X1</span><span class="p">)</span> <span class="ow">or</span> <span class="n">math</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">X2</span><span class="p">):</span>
                    <span class="n">chemical_weight</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># note: 3.3 is max deltaX between 2 elements</span>
                    <span class="n">chemical_weight</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">x_diff_weight</span> <span class="o">*</span> \
                                      <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">X1</span> <span class="o">-</span> <span class="n">X2</span><span class="p">)</span> <span class="o">/</span> <span class="mf">3.3</span><span class="p">)</span>

                <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">*</span> <span class="n">chemical_weight</span>

        <span class="c1"># sort nearest neighbors from highest to lowest weight</span>
        <span class="n">nn</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">nn</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">nn</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform_to_length</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">NNData</span><span class="p">([],</span> <span class="p">{</span><span class="mi">0</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">},</span> <span class="p">{</span><span class="mi">0</span><span class="p">:</span> <span class="p">[]}),</span>
                                            <span class="n">length</span><span class="p">)</span>

        <span class="c1"># renormalize weights so the highest weight is 1.0</span>
        <span class="n">highest_weight</span> <span class="o">=</span> <span class="n">nn</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
            <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="n">highest_weight</span>

        <span class="c1"># adjust solid angle weights based on distance</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">distance_cutoffs</span><span class="p">:</span>
            <span class="n">r1</span> <span class="o">=</span> <span class="n">_get_radius</span><span class="p">(</span><span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
                <span class="n">r2</span> <span class="o">=</span> <span class="n">_get_radius</span><span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="s2">&quot;site&quot;</span><span class="p">])</span>
                <span class="k">if</span> <span class="n">r1</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">r2</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">d</span> <span class="o">=</span> <span class="n">r1</span> <span class="o">+</span> <span class="n">r2</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s2">&quot;CrystalNN: cannot locate an appropriate radius, &quot;</span>
                        <span class="s2">&quot;covalent or atomic radii will be used, this can lead &quot;</span>
                        <span class="s2">&quot;to non-optimal results.&quot;</span><span class="p">)</span>
                    <span class="n">d</span> <span class="o">=</span> <span class="n">_get_default_radius</span><span class="p">(</span><span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">])</span> <span class="o">+</span> \
                        <span class="n">_get_default_radius</span><span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="s2">&quot;site&quot;</span><span class="p">])</span>

                <span class="n">dist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span>
                    <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">coords</span> <span class="o">-</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;site&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">coords</span><span class="p">)</span>
                <span class="n">dist_weight</span> <span class="o">=</span> <span class="mi">0</span>

                <span class="n">cutoff_low</span> <span class="o">=</span> <span class="n">d</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">distance_cutoffs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">cutoff_high</span> <span class="o">=</span> <span class="n">d</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">distance_cutoffs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

                <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;=</span> <span class="n">cutoff_low</span><span class="p">:</span>
                    <span class="n">dist_weight</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">elif</span> <span class="n">dist</span> <span class="o">&lt;</span> <span class="n">cutoff_high</span><span class="p">:</span>
                    <span class="n">dist_weight</span> <span class="o">=</span> <span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">cos</span><span class="p">((</span><span class="n">dist</span> <span class="o">-</span> <span class="n">cutoff_low</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span>
                            <span class="n">cutoff_high</span> <span class="o">-</span> <span class="n">cutoff_low</span><span class="p">)</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.5</span>
                <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">*</span> <span class="n">dist_weight</span>

        <span class="c1"># sort nearest neighbors from highest to lowest weight</span>
        <span class="n">nn</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">nn</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">nn</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform_to_length</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">NNData</span><span class="p">([],</span> <span class="p">{</span><span class="mi">0</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">},</span> <span class="p">{</span><span class="mi">0</span><span class="p">:</span> <span class="p">[]}),</span>
                                            <span class="n">length</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
            <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">],</span> <span class="mi">3</span><span class="p">)</span>
            <span class="k">del</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;poly_info&quot;</span><span class="p">]</span>  <span class="c1"># trim</span>

        <span class="c1"># remove entries with no weight</span>
        <span class="n">nn</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">nn</span> <span class="k">if</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>

        <span class="c1"># get the transition distances, i.e. all distinct weights</span>
        <span class="n">dist_bins</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">dist_bins</span> <span class="ow">or</span> <span class="n">dist_bins</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]:</span>
                <span class="n">dist_bins</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">])</span>
        <span class="n">dist_bins</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

        <span class="c1"># main algorithm to determine fingerprint from bond weights</span>
        <span class="n">cn_weights</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># CN -&gt; score for that CN</span>
        <span class="n">cn_nninfo</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># CN -&gt; list of nearneighbor info for that CN</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">dist_bins</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">val</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">nn_info</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">entry</span><span class="p">[</span><span class="s2">&quot;weight&quot;</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="n">val</span><span class="p">:</span>
                        <span class="n">nn_info</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                <span class="n">cn</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">nn_info</span><span class="p">)</span>
                <span class="n">cn_nninfo</span><span class="p">[</span><span class="n">cn</span><span class="p">]</span> <span class="o">=</span> <span class="n">nn_info</span>
                <span class="n">cn_weights</span><span class="p">[</span><span class="n">cn</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_semicircle_integral</span><span class="p">(</span><span class="n">dist_bins</span><span class="p">,</span> <span class="n">idx</span><span class="p">)</span>

        <span class="c1"># add zero coord</span>
        <span class="n">cn0_weight</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">(</span><span class="n">cn_weights</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">cn0_weight</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">cn_nninfo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">cn_weights</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">cn0_weight</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform_to_length</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">NNData</span><span class="p">(</span><span class="n">nn</span><span class="p">,</span> <span class="n">cn_weights</span><span class="p">,</span> <span class="n">cn_nninfo</span><span class="p">),</span>
                                        <span class="n">length</span><span class="p">)</span></div>

<div class="viewcode-block" id="CrystalNN.get_cn"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CrystalNN.get_cn">[docs]</a>    <span class="k">def</span> <span class="nf">get_cn</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">use_weights</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get coordination number, CN, of site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine CN.</span>
<span class="sd">            use_weights (boolean): flag indicating whether (True)</span>
<span class="sd">                to use weights for computing the coordination number</span>
<span class="sd">                or not (False, default: each coordinated site has equal</span>
<span class="sd">                weight).</span>
<span class="sd">        Returns:</span>
<span class="sd">            cn (integer or float): coordination number.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">weighted_cn</span> <span class="o">!=</span> <span class="n">use_weights</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The weighted_cn parameter and use_weights &quot;</span>
                             <span class="s2">&quot;parameter should match!&quot;</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_cn</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">use_weights</span><span class="p">)</span></div>

<div class="viewcode-block" id="CrystalNN.get_cn_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CrystalNN.get_cn_dict">[docs]</a>    <span class="k">def</span> <span class="nf">get_cn_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">use_weights</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get coordination number, CN, of each element bonded to site with index n in structure</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure</span>
<span class="sd">            n (integer): index of site for which to determine CN.</span>
<span class="sd">            use_weights (boolean): flag indicating whether (True)</span>
<span class="sd">                to use weights for computing the coordination number</span>
<span class="sd">                or not (False, default: each coordinated site has equal</span>
<span class="sd">                weight).</span>

<span class="sd">        Returns:</span>
<span class="sd">            cn (dict): dictionary of CN of each element bonded to site</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">weighted_cn</span> <span class="o">!=</span> <span class="n">use_weights</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The weighted_cn parameter and use_weights &quot;</span>
                             <span class="s2">&quot;parameter should match!&quot;</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_cn_dict</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">use_weights</span><span class="p">)</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_semicircle_integral</span><span class="p">(</span><span class="n">dist_bins</span><span class="p">,</span> <span class="n">idx</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        An internal method to get an integral between two bounds of a unit</span>
<span class="sd">        semicircle. Used in algorithm to determine bond probabilities.</span>
<span class="sd">        Args:</span>
<span class="sd">            dist_bins: (float) list of all possible bond weights</span>
<span class="sd">            idx: (float) index of starting bond weight</span>

<span class="sd">        Returns:</span>
<span class="sd">            (float) integral of portion of unit semicircle</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">r</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="n">x1</span> <span class="o">=</span> <span class="n">dist_bins</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
        <span class="n">x2</span> <span class="o">=</span> <span class="n">dist_bins</span><span class="p">[</span><span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">dist_bins</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">area1</span> <span class="o">=</span> <span class="mf">0.25</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">area1</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">((</span><span class="n">x1</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">r</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">x1</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span> <span class="o">+</span> <span class="p">(</span>
                    <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">atan</span><span class="p">(</span><span class="n">x1</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">r</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">x1</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))))</span>

        <span class="n">area2</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">((</span><span class="n">x2</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">r</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">x2</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span> <span class="o">+</span> <span class="p">(</span>
                <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">atan</span><span class="p">(</span><span class="n">x2</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">r</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">x2</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))))</span>

        <span class="k">return</span> <span class="p">(</span><span class="n">area1</span> <span class="o">-</span> <span class="n">area2</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mf">0.25</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>

<div class="viewcode-block" id="CrystalNN.transform_to_length"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CrystalNN.transform_to_length">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">transform_to_length</span><span class="p">(</span><span class="n">nndata</span><span class="p">,</span> <span class="n">length</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given NNData, transforms data to the specified fingerprint length</span>
<span class="sd">        Args:</span>
<span class="sd">            nndata: (NNData)</span>
<span class="sd">            length: (int) desired length of NNData</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">length</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">nndata</span>

        <span class="k">if</span> <span class="n">length</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">cn</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">length</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">cn</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">nndata</span><span class="o">.</span><span class="n">cn_weights</span><span class="p">:</span>
                    <span class="n">nndata</span><span class="o">.</span><span class="n">cn_weights</span><span class="p">[</span><span class="n">cn</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="n">nndata</span><span class="o">.</span><span class="n">cn_nninfo</span><span class="p">[</span><span class="n">cn</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">return</span> <span class="n">nndata</span></div></div>


<span class="k">def</span> <span class="nf">_get_default_radius</span><span class="p">(</span><span class="n">site</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An internal method to get a &quot;default&quot; covalent/element radius</span>

<span class="sd">    Args:</span>
<span class="sd">        site: (Site)</span>

<span class="sd">    Returns:</span>
<span class="sd">        Covalent radius of element on site, or Atomic radius if unavailable</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">CovalentRadius</span><span class="o">.</span><span class="n">radius</span><span class="p">[</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">symbol</span><span class="p">]</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">atomic_radius</span>


<span class="k">def</span> <span class="nf">_get_radius</span><span class="p">(</span><span class="n">site</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An internal method to get the expected radius for a site with</span>
<span class="sd">    oxidation state.</span>
<span class="sd">    Args:</span>
<span class="sd">        site: (Site)</span>

<span class="sd">    Returns:</span>
<span class="sd">        Oxidation-state dependent radius: ionic, covalent, or atomic.</span>
<span class="sd">        Returns 0 if no oxidation state or appropriate radius is found.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">,</span> <span class="s1">&#39;oxi_state&#39;</span><span class="p">):</span>
        <span class="n">el</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">element</span>
        <span class="n">oxi</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="o">.</span><span class="n">oxi_state</span>

        <span class="k">if</span> <span class="n">oxi</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">_get_default_radius</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>

        <span class="k">elif</span> <span class="n">oxi</span> <span class="ow">in</span> <span class="n">el</span><span class="o">.</span><span class="n">ionic_radii</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">el</span><span class="o">.</span><span class="n">ionic_radii</span><span class="p">[</span><span class="n">oxi</span><span class="p">]</span>

        <span class="c1"># e.g., oxi = 2.667, average together 2+ and 3+ radii</span>
        <span class="k">elif</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">oxi</span><span class="p">))</span> <span class="ow">in</span> <span class="n">el</span><span class="o">.</span><span class="n">ionic_radii</span> <span class="ow">and</span> \
                <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">oxi</span><span class="p">))</span> <span class="ow">in</span> <span class="n">el</span><span class="o">.</span><span class="n">ionic_radii</span><span class="p">:</span>
            <span class="n">oxi_low</span> <span class="o">=</span> <span class="n">el</span><span class="o">.</span><span class="n">ionic_radii</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">oxi</span><span class="p">))]</span>
            <span class="n">oxi_high</span> <span class="o">=</span> <span class="n">el</span><span class="o">.</span><span class="n">ionic_radii</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">oxi</span><span class="p">))]</span>
            <span class="n">x</span> <span class="o">=</span> <span class="n">oxi</span> <span class="o">-</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">oxi</span><span class="p">))</span>
            <span class="k">return</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="n">oxi_low</span> <span class="o">+</span> <span class="n">x</span> <span class="o">*</span> <span class="n">oxi_high</span>

        <span class="k">elif</span> <span class="n">oxi</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">el</span><span class="o">.</span><span class="n">average_cationic_radius</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">el</span><span class="o">.</span><span class="n">average_cationic_radius</span>

        <span class="k">elif</span> <span class="n">el</span><span class="o">.</span><span class="n">average_anionic_radius</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&gt;</span> <span class="n">oxi</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">el</span><span class="o">.</span><span class="n">average_anionic_radius</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
            <span class="s2">&quot;No oxidation states specified on sites! For better results, set &quot;</span>
            <span class="s2">&quot;the site oxidation states in the structure.&quot;</span>
        <span class="p">)</span>
    <span class="k">return</span> <span class="mi">0</span>


<div class="viewcode-block" id="CutOffDictNN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CutOffDictNN">[docs]</a><span class="k">class</span> <span class="nc">CutOffDictNN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A very basic NN class using a dictionary of fixed</span>
<span class="sd">    cut-off distances. Can also be used with no dictionary</span>
<span class="sd">    defined for a Null/Empty NN class.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cut_off_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            cut_off_dict (Dict[str, float]): a dictionary</span>
<span class="sd">            of cut-off distances, e.g. {(&#39;Fe&#39;,&#39;O&#39;): 2.0} for</span>
<span class="sd">            a maximum Fe-O bond length of 2.0 Angstroms.</span>
<span class="sd">            Note that if your structure is oxidation state</span>
<span class="sd">            decorated, the cut-off distances will have to</span>
<span class="sd">            explicitly include the oxidation state, e.g.</span>
<span class="sd">            {(&#39;Fe2+&#39;, &#39;O2-&#39;): 2.0}</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">cut_off_dict</span> <span class="o">=</span> <span class="n">cut_off_dict</span> <span class="ow">or</span> <span class="p">{}</span>

        <span class="c1"># for convenience</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_max_dist</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">lookup_dict</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">dict</span><span class="p">)</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">sp1</span><span class="p">,</span> <span class="n">sp2</span><span class="p">),</span> <span class="n">dist</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cut_off_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">lookup_dict</span><span class="p">[</span><span class="n">sp1</span><span class="p">][</span><span class="n">sp2</span><span class="p">]</span> <span class="o">=</span> <span class="n">dist</span>
            <span class="n">lookup_dict</span><span class="p">[</span><span class="n">sp2</span><span class="p">][</span><span class="n">sp1</span><span class="p">]</span> <span class="o">=</span> <span class="n">dist</span>
            <span class="k">if</span> <span class="n">dist</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max_dist</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_max_dist</span> <span class="o">=</span> <span class="n">dist</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lookup_dict</span> <span class="o">=</span> <span class="n">lookup_dict</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

<div class="viewcode-block" id="CutOffDictNN.from_preset"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CutOffDictNN.from_preset">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">from_preset</span><span class="p">(</span><span class="n">preset</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialise a CutOffDictNN according to a preset set of cut-offs.</span>

<span class="sd">        Args:</span>
<span class="sd">            preset (str): A preset name. The list of supported presets are:</span>

<span class="sd">                - &quot;vesta_2019&quot;: The distance cut-offs used by the VESTA</span>
<span class="sd">                  visualisation program.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A CutOffDictNN using the preset cut-off dictionary.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">preset</span> <span class="o">==</span> <span class="s1">&#39;vesta_2019&#39;</span><span class="p">:</span>
            <span class="n">cut_offs</span> <span class="o">=</span> <span class="n">loadfn</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_directory</span><span class="p">,</span> <span class="s1">&#39;vesta_cutoffs.yaml&#39;</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">CutOffDictNN</span><span class="p">(</span><span class="n">cut_off_dict</span><span class="o">=</span><span class="n">cut_offs</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unrecognised preset: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">preset</span><span class="p">))</span></div>

<div class="viewcode-block" id="CutOffDictNN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.CutOffDictNN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                sites.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a coordinated site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">site</span> <span class="o">=</span> <span class="n">structure</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>

        <span class="n">neighs_dists</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max_dist</span><span class="p">)</span>

        <span class="n">nn_info</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">nn</span> <span class="ow">in</span> <span class="n">neighs_dists</span><span class="p">:</span>
            <span class="n">n_site</span> <span class="o">=</span> <span class="n">nn</span>
            <span class="n">dist</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">nn_distance</span>
            <span class="n">neigh_cut_off_dist</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lookup_dict</span> \
                <span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">species_string</span><span class="p">,</span> <span class="p">{})</span> \
                <span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">n_site</span><span class="o">.</span><span class="n">species_string</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;</span> <span class="n">neigh_cut_off_dist</span><span class="p">:</span>
                <span class="n">nn_info</span><span class="o">.</span><span class="n">append</span><span class="p">({</span>
                    <span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">n_site</span><span class="p">,</span>
                    <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_image</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n_site</span><span class="p">),</span>
                    <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">dist</span><span class="p">,</span>
                    <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_original_site</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n_site</span><span class="p">)</span>
                <span class="p">})</span>

        <span class="k">return</span> <span class="n">nn_info</span></div></div>


<div class="viewcode-block" id="Critic2NN"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.Critic2NN">[docs]</a><span class="k">class</span> <span class="nc">Critic2NN</span><span class="p">(</span><span class="n">NearNeighbors</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Performs a topological analysis using critic2 to obtain</span>
<span class="sd">    neighbor information, using a sum of atomic charge</span>
<span class="sd">    densities. If an actual charge density is available</span>
<span class="sd">    (e.g. from a VASP CHGCAR), see Critic2Caller directly</span>
<span class="sd">    instead.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Init for Critic2NN.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># we cache the last-used structure, in case user</span>
        <span class="c1"># calls get_nn_info() repeatedly for different</span>
        <span class="c1"># sites in the same structure to save redundant</span>
        <span class="c1"># computations</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__last_structure</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__last_bonded_structure</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">structures_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Structure</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">molecules_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: can this NearNeighbors class be used with Molecule</span>
<span class="sd">        objects?</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">extend_structure_molecules</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Boolean property: Do Molecules need to be converted to Structures to use</span>
<span class="sd">        this NearNeighbors class? Note: this property is not defined for classes</span>
<span class="sd">        for which molecules_allowed == False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span>

<div class="viewcode-block" id="Critic2NN.get_bonded_structure"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.Critic2NN.get_bonded_structure">[docs]</a>    <span class="k">def</span> <span class="nf">get_bonded_structure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">decorate</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param structure: Input structure</span>
<span class="sd">        :param decorate: Whether to decorate the structure</span>
<span class="sd">        :return: Bonded structure</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># not a top-level import because critic2 is an optional</span>
        <span class="c1"># dependency, only want to raise an import error if</span>
        <span class="c1"># Critic2NN() is used</span>
        <span class="kn">from</span> <span class="nn">pymatgen.command_line.critic2_caller</span> <span class="kn">import</span> <span class="n">Critic2Caller</span>

        <span class="k">if</span> <span class="n">structure</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__last_structure</span><span class="p">:</span>
            <span class="n">sg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__last_bonded_structure</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">c2_output</span> <span class="o">=</span> <span class="n">Critic2Caller</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span><span class="o">.</span><span class="n">output</span>
            <span class="n">sg</span> <span class="o">=</span> <span class="n">c2_output</span><span class="o">.</span><span class="n">structure_graph</span><span class="p">()</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">__last_structure</span> <span class="o">=</span> <span class="n">structure</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__last_bonded_structure</span> <span class="o">=</span> <span class="n">sg</span>

        <span class="k">if</span> <span class="n">decorate</span><span class="p">:</span>
            <span class="n">order_parameters</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_local_order_parameters</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
                                <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">))]</span>
            <span class="n">sg</span><span class="o">.</span><span class="n">structure</span><span class="o">.</span><span class="n">add_site_property</span><span class="p">(</span><span class="s1">&#39;order_parameters&#39;</span><span class="p">,</span> <span class="n">order_parameters</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">sg</span></div>

<div class="viewcode-block" id="Critic2NN.get_nn_info"><a class="viewcode-back" href="../../../pymatgen.analysis.local_env.html#pymatgen.analysis.local_env.Critic2NN.get_nn_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_nn_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all near-neighbor sites as well as the associated image locations</span>
<span class="sd">        and weights of the site with index n in structure.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): input structure.</span>
<span class="sd">            n (integer): index of site for which to determine near-neighbor</span>
<span class="sd">                sites.</span>

<span class="sd">        Returns:</span>
<span class="sd">            siw (list of tuples (Site, array, float)): tuples, each one</span>
<span class="sd">                of which represents a coordinated site, its image location,</span>
<span class="sd">                and its weight.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_bonded_structure</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">[</span>
            <span class="p">{</span>
                <span class="s1">&#39;site&#39;</span><span class="p">:</span> <span class="n">connected_site</span><span class="o">.</span><span class="n">site</span><span class="p">,</span>
                <span class="s1">&#39;image&#39;</span><span class="p">:</span> <span class="n">connected_site</span><span class="o">.</span><span class="n">jimage</span><span class="p">,</span>
                <span class="s1">&#39;weight&#39;</span><span class="p">:</span> <span class="n">connected_site</span><span class="o">.</span><span class="n">weight</span><span class="p">,</span>
                <span class="s1">&#39;site_index&#39;</span><span class="p">:</span> <span class="n">connected_site</span><span class="o">.</span><span class="n">index</span>
            <span class="p">}</span> <span class="k">for</span> <span class="n">connected_site</span> <span class="ow">in</span> <span class="n">sg</span><span class="o">.</span><span class="n">get_connected_sites</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="p">]</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.local_env</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>